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
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright (c) 2014 Joyent, Inc. All rights reserved.
*/
/*
* vnd - virtual (machine) networking datapath
*
* vnd's purpose is to provide a highly performant data path for Layer 2 network
* traffic and exist side by side an active IP netstack, each servicing
* different datalinks. vnd provides many of the same capabilities as the
* current TCP/IP stack does and some specific to layer two. Specifically:
*
* o Use of the DLD fastpath
* o Packet capture hooks
* o Ability to use hardware capabilities
* o Useful interfaces for handling multiple frames
*
* The following image shows where vnd fits into today's networking stack:
*
* +---------+----------+----------+
* | libdlpi | libvnd | libsocket|
* +---------+----------+----------+
* | · · VFS |
* | VFS · VFS +----------+
* | · | sockfs |
* +---------+----------+----------+
* | | VND | IP |
* | +----------+----------+
* | DLD/DLS |
* +-------------------------------+
* | MAC |
* +-------------------------------+
* | GLDv3 |
* +-------------------------------+
*
* -----------------------------------------
* A Tale of Two Devices - DDI Device Basics
* -----------------------------------------
*
* vnd presents itself to userland as a character device; however, it also is a
* STREAMS device so that it can interface with dld and the rest of the
* networking stack. Users never interface with the STREAMs devices directly and
* they are purely an implementation detail of vnd. Opening the STREAMS device
* require kcred and as such userland cannot interact with it or push it onto
* the stream head.
*
* The main vnd character device, /dev/vnd/ctl, is a self-cloning device. Every
* clone gets its own minor number; however, minor nodes are not created in the
* devices tree for these instances. In this state a user may do two different
* things. They may issue ioctls that affect global state or they may issue
* ioctls that try to attach it to a given datalink. Once a minor device has
* been attached to a datalink, all operations on it are scoped to that context,
* therefore subsequent global operations are not permitted.
*
* A given device can be linked into the /devices and /dev name space via a link
* ioctl. That ioctl causes a minor node to be created in /devices and then it
* will also appear under /dev/vnd/ due to vnd's sdev plugin. This is similar
* to, but simpler than, IP's persistence mechanism.
*
* ---------------------
* Binding to a datalink
* ---------------------
*
* Datalinks are backed by the dld (datalink device) and dls (datalink services)
* drivers. These drivers provide a STREAMS device for datalinks on the system
* which are exposed through /dev/net. Userland generally manipulates datalinks
* through libdlpi. When an IP interface is being plumbed up what actually
* happens is that someone does a dlpi_open(3DLPI) of the underlying datalink
* and then pushes on the ip STREAMS module with an I_PUSH ioctl. Modules may
* then can negotiate with dld and dls to obtain access to various capabilities
* and fast paths via a series of STREAMS messages.
*
* In vnd, we do the same thing, but we leave our STREAMS module as an
* implementation detail of the system. We don't want users to be able to
* arbitrarily push vnd STREAMS module onto any stream, so we explicitly require
* kcred to manipulate it. Thus, when a user issues a request to attach a
* datalink to a minor instance of the character device, that vnd minor instance
* itself does a layered open (ldi_open_by_name(9F)) of the specified datalink.
* vnd does that open using the passed in credentials from the ioctl, not kcred.
* This ensures that users who doesn't have permissions to open the device
* cannot. Once that's been opened, we push on the vnd streams module.
*
* Once the vnd STREAMS instance has been created for this device, eg. the
* I_PUSH ioctl returns, we explicitly send a STREAMS ioctl
* (VND_STRIOC_ASSOCIATE) to associate the vnd STREAMS and character devices.
* This association begins the STREAM device's initialization. We start up an
* asynchronous state machine that takes care of all the different aspects of
* plumbing up the device with dld and dls and enabling the MAC fast path. We
* need to guarantee to consumers of the character device that by the time their
* ioctl returns, the data path has been fully initialized.
*
* The state progression is fairly linear. There are two general steady states.
* The first is VND_S_ONLINE, which means that everything is jacked up and good
* to go. The alternative is VND_S_ZOMBIE, which means that the streams device
* encountered an error or we have finished tearing it down and the character
* device can clean it up. The following is our state progression and the
* meaning of each state:
*
* |
* |
* V
* +---------------+
* | VNS_S_INITIAL | This is our initial state. Every
* +---------------+ vnd STREAMS device starts here.
* | While in this state, only dlpi
* | M_PROTO and M_IOCTL messages can be
* | sent or received. All STREAMS based
* | data messages are dropped.
* | We transition out of this state by
* | sending a DL_INFO_REQ to obtain
* | information about the underlying
* | link.
* v
* +-----------------+
* +--<-| VNS_S_INFO_SENT | In this state, we verify and
* | +-----------------+ record information about the
* | | underlying device. If the device is
* | | not suitable, eg. not of type
* v | DL_ETHER, then we immediately
* | | become a ZOMBIE. To leave this
* | | state we request exclusive active
* | | access to the device via
* v | DL_EXCLUSIVE_REQ.
* | v
* | +----------------------+
* +--<-| VNS_S_EXCLUSIVE_SENT | In this state, we verify whether
* | +----------------------+ or not we were able to obtain
* | | | exclusive access to the device. If
* | | | we were not able to, then we leave,
* v | | as that means that something like
* | | | IP is already plumbed up on top of
* | | | the datalink. We leave this state
* | | | by progressing through to the
* | | | appropriate DLPI primitive, either
* v | | DLPI_ATTACH_REQ or DLPI_BIND_REQ
* | | | depending on the style of the
* | | | datalink.
* | | v
* | | +-------------------+
* +------ |--<-| VNS_S_ATTACH_SENT | In this state, we verify we were
* | | +-------------------+ able to perform a standard DLPI
* | | | attach and if so, go ahead and
* v | | send a DLPI_BIND_REQ.
* | v v
* | +-------------------+
* +--<-| VNS_S_BIND_SENT | In this state we see the result of
* | +-------------------+ our attempt to bind to PPA 0 of the
* v | underlying device. Because we're
* | | trying to be a layer two datapath,
* | | the specific attachment point isn't
* | | too important as we're going to
* v | have to enable promiscuous mode. We
* | | transition out of this by sending
* | | our first of three promiscuous mode
* | | requests.
* v v
* | +------------------------+
* +--<-| VNS_S_SAP_PROMISC_SENT | In this state we verify that we
* | +------------------------+ were able to enable promiscuous
* | | mode at the physical level. We
* | | transition out of this by enabling
* | | multicast and broadcast promiscuous
* v | mode.
* | v
* | +--------------------------+
* +--<-| VNS_S_MULTI_PROMISC_SENT | In this state we verify that we
* | +--------------------------+ have enabled DL_PROMISC_MULTI and
* v | move onto the final promiscuous
* | | mode request.
* | v
* | +----------------------------+
* +--<-| VNS_S_RX_ONLY_PROMISC_SENT | In this state we verify that we
* | +----------------------------+ enabled RX_ONLY promiscuous mode.
* | | We specifically do this as we don't
* v | want to receive our own traffic
* | | that we'll send out. We leave this
* | | state by requesting the set of
* | | dld/dls capabilities that we can
* v | process.
* | |
* | v
* | +--------------------+
* +--<-| VNS_S_CAPAB_Q_SENT | We loop over the set of
* | +--------------------+ capabilities that dld advertised
* | | and enable the ones that currently
* v | support for use. See the section
* | | later on regarding capabilities
* | | for more information. We leave this
* | | state by sending an enable request.
* v v
* | +--------------------+
* +--<-| VNS_S_CAPAB_E_SENT | Here we finish all capability
* | +--------------------+ initialization. Once finished, we
* | | transition to the next state. If
* v | the dld fast path is not available,
* | | we become a zombie.
* | v
* | +--------------+
* +--<-| VNS_S_ONLINE | This is a vnd STREAMS device's
* | +--------------+ steady state. It will normally
* | | reside in this state while it is in
* | | active use. It will only transition
* v | to the next state when the STREAMS
* | | device is closed by the character
* | | device. In this state, all data
* | | flows over the dld fast path.
* | v
* | +---------------------+
* +--<-| VNS_S_SHUTTING_DOWN | This vnd state takes care of
* | +---------------------+ disabling capabilities and then
* | | transitions to zombie state to
* v | indicate that it is finished.
* | v
* | +--------------+
* +--->| VNS_S_ZOMBIE | In this state, the vnd STREAMS
* +--------------+ device is waiting to finished being
* reaped.
*
* If the stream association fails for any reason the state machine reaches
* VNS_S_ZOMBIE. A more detailed vnd_errno_t will propagate back through the
* STREAMS ioctl to the character device. That will fail the user ioctl and
* propagate the vnd_errno_t back to userland. If, on the other hand, the
* association succeeds, then the vnd STREAMS device will be fully plumbed up
* and ready to transmit and receive message blocks. Consumers will be able to
* start using the other cbops(9E) entry points once the attach has fully
* finished, which will occur after the original user attach ioctl to the
* character device returns.
*
* --------------------
* General Architecture
* --------------------
*
* There are several different devices and structures in the vnd driver. There
* is a per-netstack component, pieces related to the character device that
* consumers see, the internal STREAMS device state, and the data queues
* themselves. The following ASCII art picture describes their relationships and
* some of the major pieces of data that contain them. These are not exhaustive,
* eg. synchronization primitives are left out.
*
* +----------------+ +-----------------+
* | global | | global |
* | device list | | netstack list |
* | vnd_dev_list | | vnd_nsd_list |
* +----------------+ +-----------------+
* | |
* | v
* | +-------------------+ +-------------------+
* | | per-netstack data | ---> | per-netstack data | --> ...
* | | vnd_pnsd_t | | vnd_pnsd_t |
* | | | +-------------------+
* | | |
* | | nestackid_t ---+----> Netstack ID
* | | vnd_pnsd_flags_t -+----> Status flags
* | | zoneid_t ---+----> Zone ID for this netstack
* | | hook_family_t ---+----> VND IPv4 Hooks
* | | hook_family_t ---+----> VND IPv6 Hooks
* | | list_t ----+ |
* | +------------+------+
* | |
* | v
* | +------------------+ +------------------+
* | | character device | ---> | character device | -> ...
* +---------->| vnd_dev_t | | vnd_dev_t |
* | | +------------------+
* | |
* | minor_t ---+--> device minor number
* | ldi_handle_t ---+--> handle to /dev/net/%datalink
* | vnd_dev_flags_t -+--> device flags, non blocking, etc.
* | char[] ---+--> name if linked
* | vnd_str_t * -+ |
* +--------------+---+
* |
* v
* +-------------------------+
* | STREAMS device |
* | vnd_str_t |
* | |
* | vnd_str_state_t ---+---> State machine state
* | gsqueue_t * ---+---> mblk_t Serialization queue
* | vnd_str_stat_t ---+---> per-device kstats
* | vnd_str_capab_t ---+----------------------------+
* | vnd_data_queue_t ---+ | |
* | vnd_data_queue_t -+ | | v
* +-------------------+-+---+ +---------------------+
* | | | Stream capabilities |
* | | | vnd_str_capab_t |
* | | | |
* | | supported caps <--+-- vnd_capab_flags_t |
* | | dld cap handle <--+-- void * |
* | | direct tx func <--+-- vnd_dld_tx_t |
* | | +---------------------+
* | |
* +----------------+ +-------------+
* | |
* v v
* +-------------------+ +-------------------+
* | Read data queue | | Write data queue |
* | vnd_data_queue_t | | vnd_data_queue_t |
* | | | |
* | size_t ----+--> Current size | size_t ----+--> Current size
* | size_t ----+--> Max size | size_t ----+--> Max size
* | mblk_t * ----+--> Queue head | mblk_t * ----+--> Queue head
* | mblk_t * ----+--> Queue tail | mblk_t * ----+--> Queue tail
* +-------------------+ +-------------------+
*
*
* Globally, we maintain two lists. One list contains all of the character
* device soft states. The other maintains a list of all our netstack soft
* states. Each netstack maintains a list of active devices that have been
* associated with a datalink in its netstack.
*
* Recall that a given minor instance of the character device exists in one of
* two modes. It can either be a cloned open of /dev/vnd/ctl, the control node,
* or it can be associated with a given datalink. When minor instances are in
* the former state, they do not exist in a given vnd_pnsd_t's list of devices.
* As part of attaching to a datalink, the given vnd_dev_t will be inserted into
* the appropriate vnd_pnsd_t. In addition, this will cause a STREAMS device, a
* vnd_str_t, to be created and associated to a vnd_dev_t.
*
* The character device, and its vnd_dev_t, is the interface to the rest of the
* system. The vnd_dev_t keeps track of various aspects like whether various
* operations, such as read, write and the frameio ioctls, are considered
* blocking or non-blocking in the O_NONBLOCK sense. It also is responsible for
* keeping track of things like the name of the device, if any, in /dev. The
* vnd_str_t, on the other hand manages aspects like buffer sizes and the actual
* data queues. However, ioctls that manipulate these properties all go through
* the vnd_dev_t to its associated vnd_str_t.
*
* Each of the STREAMS devices, the vnd_str_t, maintains two data queues. One
* for frames to transmit (write queue) and one for frames received (read
* queue). These data queues have a maximum size and attempting to add data
* beyond that maximum size will result in data being dropped. The sizes are
* configurable via ioctls VND_IOC_SETTXBUF, VND_IOC_SETRXBUF. Data either sits
* in those buffers or has a reservation in those buffers while they are in vnd
* and waiting to be consumed by the user or by mac.
*
* Finally, the vnd_str_t also has a vnd_str_capab_t which we use to manage the
* available, negotiated, and currently active features.
*
* ----------------------
* Data Path and gsqueues
* ----------------------
*
* There's a lot of plumbing in vnd to get to the point where we can send data,
* but vnd's bread and butter is the data path, so it's worth diving into it in
* more detail. Data enters and exits the system from two ends.
*
* The first end is the vnd consumer. This comes in the form of read and write
* system calls as well as the frame I/O ioctls. The read and write system calls
* operate on a single frame at a time. Think of a frame as a single message
* that has come in off the wire, which may itself comprise multiple mblk_t's
* linked together in the kernel. readv(2) and writev(2) have the same
* limitations as read(2) and write(2). We enforce this as the system is
* required to fill up every uio(9S) buffer before moving onto the next one.
* This means that if you have a MTU sized buffer and two frames come in which
* are less than half of the MTU they must fill up the given iovec. Even if we
* didn't want to do this, we have no way of informing the supplier of the
* iovecs that they were only partially filled or where one frame ends and
* another begins. That's life, as such we have frame I/O which solves this
* problem. It allows for multiple frames to be consumed as well as for frames
* to be broken down into multiple vector components.
*
* The second end is the mac direct calls. As part of negotiating capabilities
* via dld, we give mac a function of ours to call when packets are received
* [vnd_mac_input()] and a callback to indicate that flow has been restored
* [vnd_mac_flow_control()]. In turn, we also get a function pointer that we can
* transmit data with. As part of the contract with mac, mac is allowed to flow
* control us by returning a cookie to the transmit function. When that happens,
* all outbound traffic is halted until our callback function is called and we
* can schedule drains.
*
* It's worth looking at these in further detail. We'll start with the rx path.
*
*
* |
* * . . . packets from gld
* |
* v
* +-------------+
* | mac |
* +-------------+
* |
* v
* +-------------+
* | dld |
* +-------------+
* |
* * . . . dld direct callback
* |
* v
* +---------------+
* | vnd_mac_input |
* +---------------+
* |
* v
* +---------+ +-------------+
* | dropped |<--*---------| vnd_hooks |
* | by | . +-------------+
* | hooks | . drop probe |
* +---------+ kstat bump * . . . Do we have free
* | buffer space?
* |
* no . | . yes
* . + .
* +---*--+------*-------+
* | |
* * . . drop probe * . . recv probe
* | kstat bump | kstat bump
* v |
* +---------+ * . . fire pollin
* | freemsg | v
* +---------+ +-----------------------+
* | vnd_str_t`vns_dq_read |
* +-----------------------+
* ^ ^
* +----------+ | | +---------+
* | read(9E) |-->-+ +--<--| frameio |
* +----------+ +---------+
*
* The rx path is rather linear. Packets come into us from mac. We always run
* them through the various hooks, and if they come out of that, we inspect the
* read data queue. If there is not enough space for a packet, we drop it.
* Otherwise, we append it to the data queue, and fire read notifications
* targetting anyone polling or doing blocking I/O on this device. Those
* consumers then drain the head of the data queue.
*
* The tx path is more complicated due to mac flow control. After any call into
* mac, we may have to potentially suspend writes and buffer data for an
* arbitrary amount of time. As such, we need to carefully track the total
* amount of outstanding data so that we don't waste kernel memory. This is
* further complicated by the fact that mac will asynchronously tell us when our
* flow has been resumed.
*
* For data to be able to enter the system, it needs to be able to take a
* reservation from the write data queue. Once the reservation has been
* obtained, we enter the gsqueue so that we can actually append it. We use
* gsqueues (serialization queues) to ensure that packets are manipulated in
* order as we deal with the draining and appending packets. We also leverage
* its worker thread to help us do draining after mac has restorted our flow.
*
* The following image describes the flow:
*
* +-----------+ +--------------+ +-------------------------+ +------+
* | write(9E) |-->| Space in the |--*--->| gsqueue_enter_one() |-->| Done |
* | frameio | | write queue? | . | +->vnd_squeue_tx_append | +------+
* +-----------+ +--------------+ . +-------------------------+
* | ^ .
* | | . reserve space from gsqueue
* | | |
* queue . . . * | space v
* full | * . . . avail +------------------------+
* v | | vnd_squeue_tx_append() |
* +--------+ +------------+ +------------------------+
* | EAGAIN |<--*------| Non-block? |<-+ |
* +--------+ . +------------+ | v
* . yes v | wait +--------------+
* no . .* * . . for | append chain |
* +----+ space | to outgoing |
* | mblk chain |
* from gsqueue +--------------+
* | |
* | +-------------------------------------------------+
* | |
* | | yes . . .
* v v .
* +-----------------------+ +--------------+ . +------+
* | vnd_squeue_tx_drain() |--->| mac blocked? |----*---->| Done |
* +-----------------------+ +--------------+ +------+
* | |
* +---------------------------------|---------------------+
* | | tx |
* | no . . * queue . . *
* | flow controlled . | empty * . fire pollout
* | . v | if mblk_t's
* +-------------+ . +---------------------+ | sent
* | set blocked |<----*------| vnd_squeue_tx_one() |--------^-------+
* | flags | +---------------------+ |
* +-------------+ More data | | | More data |
* and limit ^ v * . . and limit ^
* not reached . . * | | reached |
* +----+ | |
* v |
* +----------+ +-------------+ +---------------------------+
* | mac flow |--------->| remove mac |--->| gsqueue_enter_one() with |
* | control | | block flags | | vnd_squeue_tx_drain() and |
* | callback | +-------------+ | GSQUEUE_FILL flag, iff |
* +----------+ | not already scheduled |
* +---------------------------+
*
* The final path taken for a given write(9E)/frameio ioctl depends on whether
* or not the vnd_dev_t is non-blocking. That controls the initial path of
* trying to take a reservation in write data queue. If the device is in
* non-blocking mode, we'll return EAGAIN when there is not enough space
* available, otherwise, the calling thread blocks on the data queue.
*
* Today when we call into vnd_squeue_tx_drain() we will not try to drain the
* entire queue, as that could be quite large and we don't want to necessarily
* keep the thread that's doing the drain until it's been finished. Not only
* could more data be coming in, but the draining thread could be a userland
* thread that has more work to do. We have two limits today. There is an upper
* bound on the total amount of data and the total number of mblk_t chains. If
* we hit either limit, then we will schedule another drain in the gsqueue and
* go from there.
*
* It's worth taking some time to describe how we interact with gsqueues. vnd
* has a gsqueue_set_t for itself. It's important that it has its own set, as
* the profile of work that vnd does is different from other sub-systems in the
* kernel. When we open a STREAMS device in vnd_s_open, we get a random gsqueue.
* Unlike TCP/IP which uses an gsqueue for per TCP connection, we end up
* maintaining one for a given device. Because of that, we want to use a
* pseudo-random one to try and spread out the load, and picking one at random
* is likely to be just as good as any fancy algorithm we might come up with,
* especially as any two devices could have radically different transmit
* profiles.
*
* While some of the write path may seem complicated, it does allow us to
* maintain an important property. Once we have acknowledged a write(9E) or
* frameio ioctl, we will not drop the packet, excepting something like ipf via
* the firewall hooks.
*
* There is one other source of flow control that can exist in the system which
* is in the form of a barrier. The barrier is an internal mechanism used for
* ensuring that an gsqueue is drained for a given device. We use this as part
* of tearing down. Specifically we disable the write path so nothing new can be
* inserted into the gsqueue and then insert a barrier block. Once the barrier
* block comes out of the gsqueue, then we know nothing else in the gsqueue that
* could refer to the vnd_str_t, being destroyed, exists.
*
* ---------------------
* vnd, zones, netstacks
* ---------------------
*
* vnd devices are scoped to datalinks and datalinks are scoped to a netstack.
* Because of that, vnd is also a netstack module. It registers with the
* netstack sub-system and receives callbacks every time a netstack is created,
* being shutdown, and destroyed. The netstack callbacks drive the creation and
* destruction of the vnd_pnsd_t structures.
*
* Recall from the earlier architecture diagrams that every vnd device is scoped
* to a netstack and known about by a given vnd_pnsd_t. When that netstack is
* torn down, we also tear down any vnd devices that are hanging around. When
* the netstack is torn down, we know that any zones that are scoped to that
* netstack are being shut down and have no processes remaining. This is going
* to be the case whether they are shared or exclusive stack zones. We have to
* perform a careful dance.
*
* There are two different callbacks that happen on tear down, the first is a
* shutdown callback, the second is a destroy callback. When the shutdown
* callback is fired we need to prepare for the netstack to go away and ensure
* that nothing can continue to persist itself.
*
* More specifically, when we get notice of a stack being shutdown we first
* remove the netstack from the global netstack list to ensure that no one new
* can come in and find the netstack and get a reference to it. After that, we
* notify the neti hooks that they're going away. Once that's all done, we get
* to the heart of the matter.
*
* When shutting down there could be any number of outstanding contexts that
* have a reference on the vnd_pnsd_t and on the individual links. However, we
* know that no one new will be able to find the vnd_pnsd_t. To account for
* things that have existing references we mark the vnd_pnsd_t`vpnd_flags with
* VND_NS_CONDEMNED. This is checked by code paths that wish to append a device
* to the netstack's list. If this is set, then they must not append to it.
* Once this is set, we know that the netstack's list of devices can never grow,
* only shrink.
*
* Next, for each device we tag it with VND_D_ZONE_DYING. This indicates that
* the container for the device is being destroyed and that we should not allow
* additional references to the device to be created, whether via open, or
* linking. The presence of this bit also allows things like the list ioctl and
* sdev to know not to consider its existence. At the conclusion of this being
* set, we know that no one else should be able to obtain a new reference to the
* device.
*
* Once that has been set for all devices, we go through and remove any existing
* links that have been established in sdev. Because doing that may cause the
* final reference for the device to be dropped, which still has a reference to
* the netstack, we have to restart our walk due to dropped locks. We know that
* this walk will eventually complete because the device cannot be relinked and
* no new devices will be attached in this netstack due to VND_NS_CONDEMNED.
* Once that's finished, the shutdown callback returns.
*
* When we reach the destroy callback, we simply wait for references on the
* netstack to disappear. Because the zone has been shut down, all processes in
* it that have open references have been terminated and reaped. Any threads
* that are newly trying to reference it will fail. However, there is one thing
* that can halt this that we have no control over, which is the global zone
* holding open a reference to the device. In this case the zone halt will hang
* in vnd_stack_destroy. Once the last references is dropped we finish destroy
* the netinfo hooks and free the vnd_pnsd_t.
*
* ----
* sdev
* ----
*
* vnd registers a sdev plugin which allows it to dynamically fill out /dev/vnd
* for both the global and non-global zones. In any given zone we always supply
* a control node via /dev/vnd/ctl. This is the self-cloning node. Each zone
* will also have an entry per-link in that zone under /dev/vnd/%datalink, eg.
* if a link was named net0, there would be a /dev/vnd/net0. The global zone can
* also see every link for every zone, ala /dev/net, under
* /dev/vnd/%zonename/%datalink, eg. if a zone named 'turin' had a vnd device
* named net0, the global zone would have /dev/vnd/turin/net0.
*
* The sdev plugin has three interfaces that it supplies back to sdev. One is to
* validate that a given node is still valid. The next is a callback from sdev
* to say that it is no longer using the node. The third and final one is from
* sdev where it asks us to fill a directory. All of the heavy lifting is done
* in directory filling and in valiation. We opt not to maintain a reference on
* the device while there is an sdev node present. This makes the removal of
* nodes much simpler and most of the possible failure modes shouldn't cause any
* real problems. For example, the open path has to handle both dev_t's which no
* longer exist and which are no longer linked.
*
* -----
* hooks
* -----
*
* Like IP, vnd sends all L3 packets through its firewall hooks. Currently vnd
* provides these for L3 IP and IPv6 traffic. Each netstack provides these hooks
* in a minimal fashion. While we will allow traffic to be filtered through the
* hooks, we do not provide means for packet injection or additional inspection
* at this time. There are a total of four different events created:
*
* o IPv4 physical in
* o IPv4 physical out
* o IPv6 physical in
* o IPv6 physical out
*
* ---------------
* Synchronization
* ---------------
*
* To make our synchronization simpler, we've put more effort into making the
* metadata/setup paths do more work. That work allows the data paths to make
* assumptions around synchronization that simplify the general case. Each major
* structure, the vnd_pnsd_t, vnd_dev_t, vnd_str_t, and vnd_data_queue_t is
* annotated with the protection that its members receives. The following
* annotations are used:
*
* A Atomics; these values are only modified using atomics values.
* Currently this only applies to kstat values.
* E Existence; no lock is needed to access this member, it does not
* change while the structure is valid.
* GL Global Lock; these members are protected by the global
* vnd_dev_lock.
* L Locked; access to the member is controlled by a lock that is in
* the structure.
* NSL netstack lock; this member is protected by the containing
* netstack. This only applies to the vnd_dev_t`vdd_nslink.
* X This member is special, and is discussed in this section.
*
* In addition to locking, we also have reference counts on the vnd_dev_t and
* the vnd_pnsd_t. The reference counts describe the lifetimes of the structure.
* With rare exception, once a reference count is decremented, the consumer
* should not assume that the data is valid any more. The only exception to this
* is the case where we're removing an extant reference count from a link into
* /devices or /dev. Reference counts are obtained on these structures as a part
* of looking them up.
*
* # Global Lock Ordering
* ######################
*
* The following is the order that you must take locks in vnd:
*
* 1) vnd`vnd_dev_lock
* 2) vnd_pnsd_t`vpnd_lock
* 3) vnd_dev_t`vnd_lock
* 4) vnd_str_t`vns_lock
* 5) vnd_data_queue_t`vdq_lock
*
* One must adhere to the following rules:
*
* o You must acquire a lower numbered lock before a high numbered lock.
* o It is NOT legal to hold two locks of the same level concurrently, eg. you
* can not hold two different vnd_dev_t's vnd_lock at the same time.
* o You may release locks in any order.
* o If you release a lock, you must honor the locking rules before acquiring
* it again.
* o You should not hold any locks when calling any of the rele functions.
*
* # Special Considerations
* ########################
*
* While most of the locking is what's expected, it's worth going into the
* special nature that a few members hold. Today, only two structures have
* special considerations: the vnd_dev_t and the vnd_str_t. All members with
* special considerations have an additional annotation that describes how you
* should interact with it.
*
* vnd_dev_t: The vdd_nsd and vdd_cr are only valid when the minor node is
* attached or in the process of attaching. If the code path that goes through
* requires an attached vnd_dev_t, eg. the data path and tear down path, then it
* is always legal to dereference that member without a lock held. When they are
* added to the system, they should be done under the vdd_lock and done as part
* of setting the VND_D_ATTACH_INFLIGHT flag. These should not change during the
* lifetime of the vnd_dev_t.
*
* vnd_dev_t: The vdd_ldih is similar to the vdd_nsd and vdd_cr, except that it
* always exists as it is a part of the structure. The only time that it's valid
* to be using it is during the attach path with the VND_D_ATTACH_INFLIGHT flag
* set or during tear down. Outside of those paths which are naturally
* serialized, there is no explicit locking around the member.
*
* vnd_str_t: The vns_dev and vns_nsd work in similar ways. They are not
* initially set as part of creating the structure, but are set as part of
* responding to the association ioctl. Anything in the data path or metadata
* path that requires association may assume that they exist, as we do not kick
* off the state machine until they're set.
*
* vnd_str_t: The vns_drainblk and vns_barrierblk are similarly special. The
* members are designed to be used as part of various operations with the
* gsqueues. A lock isn't needed to use them, but to work with them, the
* appropriate flag in the vnd_str_t`vns_flags must have been set by the current
* thread. Otherwise, it is always fair game to refer to their addresses. Their
* contents are ignored by vnd, but some members are manipulated by the gsqueue
* subsystem.
*/
#include <sys/conf.h>
#include <sys/devops.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/ddi.h>
#include <sys/ethernet.h>
#include <sys/stropts.h>
#include <sys/sunddi.h>
#include <sys/stream.h>
#include <sys/strsun.h>
#include <sys/ksynch.h>
#include <sys/taskq_impl.h>
#include <sys/sdt.h>
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/dlpi.h>
#include <sys/cred.h>
#include <sys/id_space.h>
#include <sys/list.h>
#include <sys/ctype.h>
#include <sys/policy.h>
#include <sys/sunldi.h>
#include <sys/cred.h>
#include <sys/strsubr.h>
#include <sys/poll.h>
#include <sys/neti.h>
#include <sys/hook.h>
#include <sys/hook_event.h>
#include <sys/vlan.h>
#include <sys/dld.h>
#include <sys/mac_client.h>
#include <sys/netstack.h>
#include <sys/fs/sdev_plugin.h>
#include <sys/kstat.h>
#include <sys/atomic.h>
#include <sys/disp.h>
#include <sys/random.h>
#include <sys/gsqueue.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <sys/vnd.h>
/*
* Globals
*/
static dev_info_t *vnd_dip;
static taskq_t *vnd_taskq;
static kmem_cache_t *vnd_str_cache;
static kmem_cache_t *vnd_dev_cache;
static kmem_cache_t *vnd_pnsd_cache;
static id_space_t *vnd_minors;
static int vnd_list_init = 0;
static sdev_plugin_hdl_t vnd_sdev_hdl;
static gsqueue_set_t *vnd_sqset;
static kmutex_t vnd_dev_lock;
static list_t vnd_dev_list; /* Protected by the vnd_dev_lock */
static list_t vnd_nsd_list; /* Protected by the vnd_dev_lock */
/*
* STREAMs ioctls
*
* The STREAMs ioctls are internal to vnd. No one should be seeing them, as such
* they aren't a part of the header file.
*/
#define VND_STRIOC (('v' << 24) | ('n' << 16) | ('d' << 8) | 0x80)
/*
* Private ioctl to associate a given streams instance with a minor instance of
* the character device.
*/
#define VND_STRIOC_ASSOCIATE (VND_STRIOC | 0x1)
typedef struct vnd_strioc_associate {
minor_t vsa_minor; /* minor device node */
netstackid_t vsa_nsid; /* netstack id */
vnd_errno_t vsa_errno; /* errno */
} vnd_strioc_associate_t;
typedef enum vnd_strioc_state {
VSS_UNKNOWN = 0,
VSS_COPYIN = 1,
VSS_COPYOUT = 2,
} vnd_strioc_state_t;
typedef struct vnd_strioc {
vnd_strioc_state_t vs_state;
caddr_t vs_addr;
} vnd_strioc_t;
/*
* VND SQUEUE TAGS, start at 0x42 so we don't overlap with extent tags. Though
* really, overlap is at the end of the day, inevitable.
*/
#define VND_SQUEUE_TAG_TX_DRAIN 0x42
#define VND_SQUEUE_TAG_MAC_FLOW_CONTROL 0x43
#define VND_SQUEUE_TAG_VND_WRITE 0x44
#define VND_SQUEUE_TAG_ND_FRAMEIO_WRITE 0x45
#define VND_SQUEUE_TAG_STRBARRIER 0x46
/*
* vnd reserved names. These are names which are reserved by vnd and thus
* shouldn't be used by some external program.
*/
static char *vnd_reserved_names[] = {
"ctl",
"zone",
NULL
};
/*
* vnd's DTrace probe macros
*
* DTRACE_VND* are all for a stable provider. We also have an unstable internal
* set of probes for reference count manipulation.
*/
#define DTRACE_VND3(name, type1, arg1, type2, arg2, type3, arg3) \
DTRACE_PROBE3(__vnd_##name, type1, arg1, type2, arg2, type3, arg3);
#define DTRACE_VND4(name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
DTRACE_PROBE4(__vnd_##name, type1, arg1, type2, arg2, type3, arg3, \
type4, arg4);
#define DTRACE_VND5(name, type1, arg1, type2, arg2, type3, arg3, \
type4, arg4, type5, arg5) \
DTRACE_PROBE5(__vnd_##name, type1, arg1, type2, arg2, type3, arg3, \
type4, arg4, type5, arg5);
#define DTRACE_VND_REFINC(vdp) \
DTRACE_PROBE2(vnd__ref__inc, vnd_dev_t *, vdp, int, vdp->vdd_ref);
#define DTRACE_VND_REFDEC(vdp) \
DTRACE_PROBE2(vnd__ref__dec, vnd_dev_t *, vdp, int, vdp->vdd_ref);
/*
* Tunables
*/
size_t vnd_vdq_default_size = 1024 * 64; /* 64 KB */
size_t vnd_vdq_hard_max = 1024 * 1024 * 4; /* 4 MB */
/*
* These numbers are designed as per-device tunables that are applied when a new
* vnd device is attached. They're a rough stab at what may be a reasonable
* amount of work to do in one burst in an squeue.
*/
size_t vnd_flush_burst_size = 1520 * 10; /* 10 1500 MTU packets */
size_t vnd_flush_nburst = 10; /* 10 frames */
/*
* Constants related to our sdev plugins
*/
#define VND_SDEV_NAME "vnd"
#define VND_SDEV_ROOT "/dev/vnd"
#define VND_SDEV_ZROOT "/dev/vnd/zone"
/*
* Statistic macros
*/
#define VND_STAT_INC(vsp, field, val) \
atomic_add_64(&(vsp)->vns_ksdata.field.value.ui64, val)
#define VND_LATENCY_1MS 1000000
#define VND_LATENCY_10MS 10000000
#define VND_LATENCY_100MS 100000000
#define VND_LATENCY_1S 1000000000
#define VND_LATENCY_10S 10000000000
/*
* Constants for vnd hooks
*/
static uint8_t vnd_bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
#define IPV4_MCAST_LEN 3
static uint8_t vnd_ipv4_mcast[3] = { 0x01, 0x00, 0x5E };
#define IPV6_MCAST_LEN 2
static uint8_t vnd_ipv6_mcast[2] = { 0x33, 0x33 };
/*
* vnd internal data structures and types
*/
struct vnd_str;
struct vnd_dev;
struct vnd_pnsd;
/*
* As part of opening the device stream we need to properly communicate with our
* underlying stream. This is a bit of an asynchronous dance and we need to
* properly work with dld to get everything set up. We have to initiate the
* conversation with dld and as such we keep track of our state here.
*/
typedef enum vnd_str_state {
VNS_S_INITIAL = 0,
VNS_S_INFO_SENT,
VNS_S_EXCLUSIVE_SENT,
VNS_S_ATTACH_SENT,
VNS_S_BIND_SENT,
VNS_S_SAP_PROMISC_SENT,
VNS_S_MULTI_PROMISC_SENT,
VNS_S_RX_ONLY_PROMISC_SENT,
VNS_S_CAPAB_Q_SENT,
VNS_S_CAPAB_E_SENT,
VNS_S_ONLINE,
VNS_S_SHUTTING_DOWN,
VNS_S_ZOMBIE
} vnd_str_state_t;
typedef enum vnd_str_flags {
VNS_F_NEED_ZONE = 0x1,
VNS_F_TASKQ_DISPATCHED = 0x2,
VNS_F_CONDEMNED = 0x4,
VNS_F_FLOW_CONTROLLED = 0x8,
VNS_F_DRAIN_SCHEDULED = 0x10,
VNS_F_BARRIER = 0x20,
VNS_F_BARRIER_DONE = 0x40
} vnd_str_flags_t;
typedef enum vnd_capab_flags {
VNS_C_HCKSUM = 0x1,
VNS_C_DLD = 0x2,
VNS_C_DIRECT = 0x4,
VNS_C_HCKSUM_BADVERS = 0x8
} vnd_capab_flags_t;
/*
* Definitions to interact with direct callbacks
*/
typedef void (*vnd_rx_t)(struct vnd_str *, mac_resource_t *, mblk_t *,
mac_header_info_t *);
typedef uintptr_t vnd_mac_cookie_t;
/* DLD Direct capability function */
typedef int (*vnd_dld_cap_t)(void *, uint_t, void *, uint_t);
/* DLD Direct tx function */
typedef vnd_mac_cookie_t (*vnd_dld_tx_t)(void *, mblk_t *, uint64_t, uint16_t);
/* DLD Direct function to set flow control callback */
typedef void *(*vnd_dld_set_fcb_t)(void *, void (*)(void *, vnd_mac_cookie_t),
void *);
/* DLD Direct function to see if flow controlled still */
typedef int (*vnd_dld_is_fc_t)(void *, vnd_mac_cookie_t);
/*
* The vnd_str_capab_t is always protected by the vnd_str_t it's a member of.
*/
typedef struct vnd_str_capab {
vnd_capab_flags_t vsc_flags;
t_uscalar_t vsc_hcksum_opts;
vnd_dld_cap_t vsc_capab_f;
void *vsc_capab_hdl;
vnd_dld_tx_t vsc_tx_f;
void *vsc_tx_hdl;
vnd_dld_set_fcb_t vsc_set_fcb_f;
void *vsc_set_fcb_hdl;
vnd_dld_is_fc_t vsc_is_fc_f;
void *vsc_is_fc_hdl;
vnd_mac_cookie_t vsc_fc_cookie;
void *vsc_tx_fc_hdl;
} vnd_str_capab_t;
/*
* The vnd_data_queue is a simple construct for storing a series of messages in
* a queue.
*
* See synchronization section of the big theory statement for member
* annotations.
*/
typedef struct vnd_data_queue {
struct vnd_str *vdq_vns; /* E */
kmutex_t vdq_lock;
kcondvar_t vdq_ready; /* Uses vdq_lock */
ssize_t vdq_max; /* L */
ssize_t vdq_cur; /* L */
mblk_t *vdq_head; /* L */
mblk_t *vdq_tail; /* L */
} vnd_data_queue_t;
typedef struct vnd_str_stat {
kstat_named_t vks_rbytes;
kstat_named_t vks_rpackets;
kstat_named_t vks_obytes;
kstat_named_t vks_opackets;
kstat_named_t vks_nhookindrops;
kstat_named_t vks_nhookoutdrops;
kstat_named_t vks_ndlpidrops;
kstat_named_t vks_ndataindrops;
kstat_named_t vks_ndataoutdrops;
kstat_named_t vks_tdrops;
kstat_named_t vks_linkname;
kstat_named_t vks_zonename;
kstat_named_t vks_nmacflow;
kstat_named_t vks_tmacflow;
kstat_named_t vks_mac_flow_1ms;
kstat_named_t vks_mac_flow_10ms;
kstat_named_t vks_mac_flow_100ms;
kstat_named_t vks_mac_flow_1s;
kstat_named_t vks_mac_flow_10s;
} vnd_str_stat_t;
/*
* vnd stream structure
*
* See synchronization section of the big theory statement for member
* annotations.
*/
typedef struct vnd_str {
kmutex_t vns_lock;
kcondvar_t vns_cancelcv; /* Uses vns_lock */
kcondvar_t vns_barriercv; /* Uses vns_lock */
kcondvar_t vns_stcv; /* Uses vns_lock */
vnd_str_state_t vns_state; /* L */
vnd_str_state_t vns_laststate; /* L */
vnd_errno_t vns_errno; /* L */
vnd_str_flags_t vns_flags; /* L */
vnd_str_capab_t vns_caps; /* L */
taskq_ent_t vns_tqe; /* L */
vnd_data_queue_t vns_dq_read; /* E */
vnd_data_queue_t vns_dq_write; /* E */
mblk_t *vns_dlpi_inc; /* L */
queue_t *vns_rq; /* E */
queue_t *vns_wq; /* E */
queue_t *vns_lrq; /* E */
t_uscalar_t vns_dlpi_style; /* L */
t_uscalar_t vns_minwrite; /* L */
t_uscalar_t vns_maxwrite; /* L */
hrtime_t vns_fclatch; /* L */
hrtime_t vns_fcupdate; /* L */
kstat_t *vns_kstat; /* E */
gsqueue_t *vns_squeue; /* E */
mblk_t vns_drainblk; /* E + X */
mblk_t vns_barrierblk; /* E + X */
vnd_str_stat_t vns_ksdata; /* A */
size_t vns_nflush; /* L */
size_t vns_bsize; /* L */
struct vnd_dev *vns_dev; /* E + X */
struct vnd_pnsd *vns_nsd; /* E + X */
} vnd_str_t;
typedef enum vnd_dev_flags {
VND_D_ATTACH_INFLIGHT = 0x001,
VND_D_ATTACHED = 0x002,
VND_D_LINK_INFLIGHT = 0x004,
VND_D_LINKED = 0x008,
VND_D_CONDEMNED = 0x010,
VND_D_ZONE_DYING = 0x020,
VND_D_OPENED = 0x040
} vnd_dev_flags_t;
/*
* This represents the data associated with a minor device instance.
*
* See synchronization section of the big theory statement for member
* annotations.
*/
typedef struct vnd_dev {
kmutex_t vdd_lock;
list_node_t vdd_link; /* GL */
list_node_t vdd_nslink; /* NSL */
int vdd_ref; /* L */
vnd_dev_flags_t vdd_flags; /* L */
minor_t vdd_minor; /* E */
dev_t vdd_devid; /* E */
ldi_ident_t vdd_ldiid; /* E */
ldi_handle_t vdd_ldih; /* X */
cred_t *vdd_cr; /* X */
vnd_str_t *vdd_str; /* L */
struct pollhead vdd_ph; /* E */
struct vnd_pnsd *vdd_nsd; /* E + X */
char vdd_datalink[VND_NAMELEN]; /* L */
char vdd_lname[VND_NAMELEN]; /* L */
} vnd_dev_t;
typedef enum vnd_pnsd_flags {
VND_NS_CONDEMNED = 0x1
} vnd_pnsd_flags_t;
/*
* Per netstack data structure.
*
* See synchronization section of the big theory statement for member
* annotations.
*/
typedef struct vnd_pnsd {
list_node_t vpnd_link; /* protected by global dev lock */
zoneid_t vpnd_zid; /* E */
netstackid_t vpnd_nsid; /* E */
boolean_t vpnd_hooked; /* E */
net_handle_t vpnd_neti_v4; /* E */
hook_family_t vpnd_family_v4; /* E */
hook_event_t vpnd_event_in_v4; /* E */
hook_event_t vpnd_event_out_v4; /* E */
hook_event_token_t vpnd_token_in_v4; /* E */
hook_event_token_t vpnd_token_out_v4; /* E */
net_handle_t vpnd_neti_v6; /* E */
hook_family_t vpnd_family_v6; /* E */
hook_event_t vpnd_event_in_v6; /* E */
hook_event_t vpnd_event_out_v6; /* E */
hook_event_token_t vpnd_token_in_v6; /* E */
hook_event_token_t vpnd_token_out_v6; /* E */
kmutex_t vpnd_lock; /* Protects remaining members */
kcondvar_t vpnd_ref_change; /* Uses vpnd_lock */
int vpnd_ref; /* L */
vnd_pnsd_flags_t vpnd_flags; /* L */
list_t vpnd_dev_list; /* L */
} vnd_pnsd_t;
static void vnd_squeue_tx_drain(void *, mblk_t *, gsqueue_t *, void *);
/*
* Drop function signature.
*/
typedef void (*vnd_dropper_f)(vnd_str_t *, mblk_t *, const char *);
static void
vnd_drop_ctl(vnd_str_t *vsp, mblk_t *mp, const char *reason)
{
DTRACE_VND4(drop__ctl, mblk_t *, mp, vnd_str_t *, vsp, mblk_t *,
mp, const char *, reason);
if (mp != NULL) {
freemsg(mp);
}
VND_STAT_INC(vsp, vks_ndlpidrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
}
static void
vnd_drop_in(vnd_str_t *vsp, mblk_t *mp, const char *reason)
{
DTRACE_VND4(drop__in, mblk_t *, mp, vnd_str_t *, vsp, mblk_t *,
mp, const char *, reason);
if (mp != NULL) {
freemsg(mp);
}
VND_STAT_INC(vsp, vks_ndataindrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
}
static void
vnd_drop_out(vnd_str_t *vsp, mblk_t *mp, const char *reason)
{
DTRACE_VND4(drop__out, mblk_t *, mp, vnd_str_t *, vsp, mblk_t *,
mp, const char *, reason);
if (mp != NULL) {
freemsg(mp);
}
VND_STAT_INC(vsp, vks_ndataoutdrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
}
static void
vnd_drop_hook_in(vnd_str_t *vsp, mblk_t *mp, const char *reason)
{
DTRACE_VND4(drop__in, mblk_t *, mp, vnd_str_t *, vsp, mblk_t *,
mp, const char *, reason);
if (mp != NULL) {
freemsg(mp);
}
VND_STAT_INC(vsp, vks_nhookindrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
}
static void
vnd_drop_hook_out(vnd_str_t *vsp, mblk_t *mp, const char *reason)
{
DTRACE_VND4(drop__out, mblk_t *, mp, vnd_str_t *, vsp, mblk_t *,
mp, const char *, reason);
if (mp != NULL) {
freemsg(mp);
}
VND_STAT_INC(vsp, vks_nhookoutdrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
}
static void
vnd_drop_panic(vnd_str_t *vsp, mblk_t *mp, const char *reason)
{
panic("illegal vnd drop");
}
static void
vnd_mac_drop_input(vnd_str_t *vsp, mac_resource_t *unused, mblk_t *mp_chain,
mac_header_info_t *mhip)
{
mblk_t *mp;
while (mp_chain != NULL) {
mp = mp_chain;
mp_chain = mp->b_next;
vnd_drop_hook_in(vsp, mp, "stream not associated");
}
}
static vnd_pnsd_t *
vnd_nsd_lookup(netstackid_t nsid)
{
vnd_pnsd_t *nsp;
mutex_enter(&vnd_dev_lock);
for (nsp = list_head(&vnd_nsd_list); nsp != NULL;
nsp = list_next(&vnd_nsd_list, nsp)) {
if (nsp->vpnd_nsid == nsid) {
mutex_enter(&nsp->vpnd_lock);
VERIFY(nsp->vpnd_ref >= 0);
nsp->vpnd_ref++;
mutex_exit(&nsp->vpnd_lock);
break;
}
}
mutex_exit(&vnd_dev_lock);
return (nsp);
}
static vnd_pnsd_t *
vnd_nsd_lookup_by_zid(zoneid_t zid)
{
netstack_t *ns;
vnd_pnsd_t *nsp;
ns = netstack_find_by_zoneid(zid);
if (ns == NULL)
return (NULL);
nsp = vnd_nsd_lookup(ns->netstack_stackid);
netstack_rele(ns);
return (nsp);
}
static vnd_pnsd_t *
vnd_nsd_lookup_by_zonename(char *zname)
{
zone_t *zonep;
vnd_pnsd_t *nsp;
zonep = zone_find_by_name(zname);
if (zonep == NULL)
return (NULL);
nsp = vnd_nsd_lookup_by_zid(zonep->zone_id);
zone_rele(zonep);
return (nsp);
}
static void
vnd_nsd_ref(vnd_pnsd_t *nsp)
{
mutex_enter(&nsp->vpnd_lock);
/*
* This can only be used on something that has been obtained through
* some other means. As such, the caller should already have a reference
* before adding another one. This function should not be used as a
* means of creating the initial reference.
*/
VERIFY(nsp->vpnd_ref > 0);
nsp->vpnd_ref++;
mutex_exit(&nsp->vpnd_lock);
cv_broadcast(&nsp->vpnd_ref_change);
}
static void
vnd_nsd_rele(vnd_pnsd_t *nsp)
{
mutex_enter(&nsp->vpnd_lock);
VERIFY(nsp->vpnd_ref > 0);
nsp->vpnd_ref--;
mutex_exit(&nsp->vpnd_lock);
cv_broadcast(&nsp->vpnd_ref_change);
}
static vnd_dev_t *
vnd_dev_lookup(minor_t m)
{
vnd_dev_t *vdp;
mutex_enter(&vnd_dev_lock);
for (vdp = list_head(&vnd_dev_list); vdp != NULL;
vdp = list_next(&vnd_dev_list, vdp)) {
if (vdp->vdd_minor == m) {
mutex_enter(&vdp->vdd_lock);
VERIFY(vdp->vdd_ref > 0);
vdp->vdd_ref++;
DTRACE_VND_REFINC(vdp);
mutex_exit(&vdp->vdd_lock);
break;
}
}
mutex_exit(&vnd_dev_lock);
return (vdp);
}
static void
vnd_dev_free(vnd_dev_t *vdp)
{
/*
* When the STREAM exists we need to go through and make sure
* communication gets torn down. As part of closing the stream, we
* guarantee that nothing else should be able to enter the stream layer
* at this point. That means no one should be able to call
* read(),write() or one of the frameio ioctls.
*/
if (vdp->vdd_flags & VND_D_ATTACHED) {
ldi_close(vdp->vdd_ldih, FREAD | FWRITE, vdp->vdd_cr);
crfree(vdp->vdd_cr);
vdp->vdd_cr = NULL;
/*
* We have to remove ourselves from our parents list now. It is
* really quite important that we have already set the condemend
* flag here so that our containing netstack basically knows
* that we're on the way down and knows not to wait for us. It's
* also important that we do that before we put a rele on the
* the device as that is the point at which it will check again.
*/
mutex_enter(&vdp->vdd_nsd->vpnd_lock);
list_remove(&vdp->vdd_nsd->vpnd_dev_list, vdp);
mutex_exit(&vdp->vdd_nsd->vpnd_lock);
vnd_nsd_rele(vdp->vdd_nsd);
vdp->vdd_nsd = NULL;
}
ASSERT(vdp->vdd_flags & VND_D_CONDEMNED);
id_free(vnd_minors, vdp->vdd_minor);
mutex_destroy(&vdp->vdd_lock);
kmem_cache_free(vnd_dev_cache, vdp);
}
static void
vnd_dev_ref(vnd_dev_t *vdp)
{
mutex_enter(&vdp->vdd_lock);
VERIFY(vdp->vdd_ref > 0);
vdp->vdd_ref++;
DTRACE_VND_REFINC(vdp);
mutex_exit(&vdp->vdd_lock);
}
/*
* As part of releasing the hold on this we may tear down a given vnd_dev_t As
* such we need to make sure that we grab the list lock first before grabbing
* the vnd_dev_t's lock to ensure proper lock ordering.
*/
static void
vnd_dev_rele(vnd_dev_t *vdp)
{
mutex_enter(&vnd_dev_lock);
mutex_enter(&vdp->vdd_lock);
VERIFY(vdp->vdd_ref > 0);
vdp->vdd_ref--;
DTRACE_VND_REFDEC(vdp);
if (vdp->vdd_ref > 0) {
mutex_exit(&vdp->vdd_lock);
mutex_exit(&vnd_dev_lock);
return;
}
/*
* Now that we've removed this from the list, we can go ahead and
* drop the list lock. No one else can find this device and reference
* it. As its reference count is zero, it by definition does not have
* any remaining entries in /devices that could lead someone back to
* this.
*/
vdp->vdd_flags |= VND_D_CONDEMNED;
list_remove(&vnd_dev_list, vdp);
mutex_exit(&vdp->vdd_lock);
mutex_exit(&vnd_dev_lock);
vnd_dev_free(vdp);
}
/*
* Insert a mesage block chain if there's space, otherwise drop it. Return one
* so someone who was waiting for data would now end up having found it. eg.
* caller should consider a broadcast.
*/
static int
vnd_dq_push(vnd_data_queue_t *vqp, mblk_t *mp, boolean_t reserved,
vnd_dropper_f dropf)
{
size_t msize;
ASSERT(MUTEX_HELD(&vqp->vdq_lock));
if (reserved == B_FALSE) {
msize = msgsize(mp);
if (vqp->vdq_cur + msize > vqp->vdq_max) {
dropf(vqp->vdq_vns, mp, "buffer full");
return (0);
}
vqp->vdq_cur += msize;
}
if (vqp->vdq_head == NULL) {
ASSERT(vqp->vdq_tail == NULL);
vqp->vdq_head = mp;
vqp->vdq_tail = mp;
} else {
vqp->vdq_tail->b_next = mp;
vqp->vdq_tail = mp;
}
return (1);
}
/*
* Remove a message message block chain. If the amount of space in the buffer
* has changed we return 1. We have no way of knowing whether or not there is
* enough space overall for a given writer who is blocked, so we always end up
* having to return true and thus tell consumers that they should consider
* signalling.
*/
static int
vnd_dq_pop(vnd_data_queue_t *vqp, mblk_t **mpp)
{
size_t msize;
mblk_t *mp;
ASSERT(MUTEX_HELD(&vqp->vdq_lock));
ASSERT(mpp != NULL);
if (vqp->vdq_head == NULL) {
ASSERT(vqp->vdq_tail == NULL);
*mpp = NULL;
return (0);
}
mp = vqp->vdq_head;
msize = msgsize(mp);
vqp->vdq_cur -= msize;
if (mp->b_next == NULL) {
vqp->vdq_head = NULL;
vqp->vdq_tail = NULL;
/*
* We can't be certain that this is always going to be zero.
* Someone may have basically taken a reservation of space on
* the data queue, eg. claimed spae but not yet pushed it on
* yet.
*/
ASSERT(vqp->vdq_cur >= 0);
} else {
vqp->vdq_head = mp->b_next;
ASSERT(vqp->vdq_cur > 0);
}
mp->b_next = NULL;
*mpp = mp;
return (1);
}
/*
* Reserve space in the queue. This will bump up the size of the queue and
* entitle the user to push something on later without bumping the space.
*/
static int
vnd_dq_reserve(vnd_data_queue_t *vqp, ssize_t size)
{
ASSERT(MUTEX_HELD(&vqp->vdq_lock));
ASSERT(size >= 0);
if (size == 0)
return (0);
if (size + vqp->vdq_cur > vqp->vdq_max)
return (0);
vqp->vdq_cur += size;
return (1);
}
static void
vnd_dq_unreserve(vnd_data_queue_t *vqp, ssize_t size)
{
ASSERT(MUTEX_HELD(&vqp->vdq_lock));
ASSERT(size > 0);
ASSERT(size <= vqp->vdq_cur);
vqp->vdq_cur -= size;
}
static void
vnd_dq_flush(vnd_data_queue_t *vqp, vnd_dropper_f dropf)
{
mblk_t *mp, *next;
mutex_enter(&vqp->vdq_lock);
for (mp = vqp->vdq_head; mp != NULL; mp = next) {
next = mp->b_next;
mp->b_next = NULL;
dropf(vqp->vdq_vns, mp, "vnd_dq_flush");
}
vqp->vdq_cur = 0;
vqp->vdq_head = NULL;
vqp->vdq_tail = NULL;
mutex_exit(&vqp->vdq_lock);
}
static boolean_t
vnd_dq_is_empty(vnd_data_queue_t *vqp)
{
boolean_t ret;
mutex_enter(&vqp->vdq_lock);
if (vqp->vdq_head == NULL)
ret = B_TRUE;
else
ret = B_FALSE;
mutex_exit(&vqp->vdq_lock);
return (ret);
}
/*
* Get a network uint16_t from the message and translate it into something the
* host understands.
*/
static int
vnd_mbc_getu16(mblk_t *mp, off_t off, uint16_t *out)
{
size_t mpsize;
uint8_t *bp;
mpsize = msgsize(mp);
/* Check for overflow */
if (off + sizeof (uint16_t) > mpsize)
return (1);
mpsize = MBLKL(mp);
while (off >= mpsize) {
mp = mp->b_cont;
off -= mpsize;
mpsize = MBLKL(mp);
}
/*
* Data is in network order. Note the second byte of data might be in
* the next mp.
*/
bp = mp->b_rptr + off;
*out = *bp << 8;
if (off + 1 == mpsize) {
mp = mp->b_cont;
bp = mp->b_rptr;
} else {
bp++;
}
*out |= *bp;
return (0);
}
/*
* Given an mblk chain find the mblk and address of a particular offset.
*/
static int
vnd_mbc_getoffset(mblk_t *mp, off_t off, mblk_t **mpp, uintptr_t *offp)
{
size_t mpsize;
if (off >= msgsize(mp))
return (1);
mpsize = MBLKL(mp);
while (off >= mpsize) {
mp = mp->b_cont;
off -= mpsize;
mpsize = MBLKL(mp);
}
*mpp = mp;
*offp = (uintptr_t)mp->b_rptr + off;
return (0);
}
/*
* Fetch the destination mac address. Set *dstp to that mac address. If the data
* is not contiguous in the first mblk_t, fill in datap and set *dstp to it.
*/
static int
vnd_mbc_getdstmac(mblk_t *mp, uint8_t **dstpp, uint8_t *datap)
{
int i;
if (MBLKL(mp) >= ETHERADDRL) {
*dstpp = mp->b_rptr;
return (0);
}
*dstpp = datap;
for (i = 0; i < ETHERADDRL; i += 2, datap += 2) {
if (vnd_mbc_getu16(mp, i, (uint16_t *)datap) != 0)
return (1);
}
return (0);
}
static int
vnd_hook(vnd_str_t *vsp, mblk_t **mpp, net_handle_t netiv4, hook_event_t hev4,
hook_event_token_t hetv4, net_handle_t netiv6, hook_event_t hev6,
hook_event_token_t hetv6, vnd_dropper_f hdrop, vnd_dropper_f ddrop)
{
uint16_t etype;
int vlan = 0;
hook_pkt_event_t info;
size_t offset, mblen;
uint8_t *dstp;
uint8_t dstaddr[6];
hook_event_t he;
hook_event_token_t het;
net_handle_t neti;
/*
* Before we can ask if we're interested we have to do enough work to
* determine the ethertype.
*/
/* Byte 12 is either the VLAN tag or the ethertype */
if (vnd_mbc_getu16(*mpp, 12, &etype) != 0) {
ddrop(vsp, *mpp, "packet has incomplete ethernet header");
*mpp = NULL;
return (1);
}
if (etype == ETHERTYPE_VLAN) {
vlan = 1;
/* Actual ethertype is another four bytes in */
if (vnd_mbc_getu16(*mpp, 16, &etype) != 0) {
ddrop(vsp, *mpp,
"packet has incomplete ethernet vlan header");
*mpp = NULL;
return (1);
}
offset = sizeof (struct ether_vlan_header);
} else {
offset = sizeof (struct ether_header);
}
/*
* At the moment we only hook on the kinds of things that the IP module
* would normally.
*/
if (etype != ETHERTYPE_IP && etype != ETHERTYPE_IPV6)
return (0);
if (etype == ETHERTYPE_IP) {
neti = netiv4;
he = hev4;
het = hetv4;
} else {
neti = netiv6;
he = hev6;
het = hetv6;
}
if (!he.he_interested)
return (0);
if (vnd_mbc_getdstmac(*mpp, &dstp, dstaddr) != 0) {
ddrop(vsp, *mpp, "packet has incomplete ethernet header");
*mpp = NULL;
return (1);
}
/*
* Now that we know we're interested, we have to do some additional
* sanity checking for IPF's sake, ala ip_check_length(). Specifically
* we need to check to make sure that the remaining packet size,
* excluding MAC, is at least the size of an IP header.
*/
mblen = msgsize(*mpp);
if ((etype == ETHERTYPE_IP &&
mblen - offset < IP_SIMPLE_HDR_LENGTH) ||
(etype == ETHERTYPE_IPV6 && mblen - offset < IPV6_HDR_LEN)) {
ddrop(vsp, *mpp, "packet has invalid IP header");
*mpp = NULL;
return (1);
}
info.hpe_protocol = neti;
info.hpe_ifp = (phy_if_t)vsp;
info.hpe_ofp = (phy_if_t)vsp;
info.hpe_mp = mpp;
info.hpe_flags = 0;
if (bcmp(vnd_bcast_addr, dstp, ETHERADDRL) == 0)
info.hpe_flags |= HPE_BROADCAST;
else if (etype == ETHERTYPE_IP &&
bcmp(vnd_ipv4_mcast, vnd_bcast_addr, IPV4_MCAST_LEN) == 0)
info.hpe_flags |= HPE_MULTICAST;
else if (etype == ETHERTYPE_IPV6 &&
bcmp(vnd_ipv6_mcast, vnd_bcast_addr, IPV6_MCAST_LEN) == 0)
info.hpe_flags |= HPE_MULTICAST;
if (vnd_mbc_getoffset(*mpp, offset, &info.hpe_mb,
(uintptr_t *)&info.hpe_hdr) != 0) {
ddrop(vsp, *mpp, "packet too small -- "
"unable to find payload");
*mpp = NULL;
return (1);
}
if (hook_run(neti->netd_hooks, het, (hook_data_t)&info) != 0) {
hdrop(vsp, *mpp, "drooped by hooks");
return (1);
}
return (0);
}
/*
* This should not be used for DL_INFO_REQ.
*/
static mblk_t *
vnd_dlpi_alloc(size_t len, t_uscalar_t prim)
{
mblk_t *mp;
mp = allocb(len, BPRI_MED);
if (mp == NULL)
return (NULL);
mp->b_datap->db_type = M_PROTO;
mp->b_wptr = mp->b_rptr + len;
bzero(mp->b_rptr, len);
((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
return (mp);
}
static void
vnd_dlpi_inc_push(vnd_str_t *vsp, mblk_t *mp)
{
mblk_t **mpp;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
ASSERT(mp->b_next == NULL);
mpp = &vsp->vns_dlpi_inc;
while (*mpp != NULL)
mpp = &((*mpp)->b_next);
*mpp = mp;
}
static mblk_t *
vnd_dlpi_inc_pop(vnd_str_t *vsp)
{
mblk_t *mp;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vsp->vns_dlpi_inc;
if (mp != NULL) {
VERIFY(mp->b_next == NULL || mp->b_next != mp);
vsp->vns_dlpi_inc = mp->b_next;
mp->b_next = NULL;
}
return (mp);
}
static int
vnd_st_sinfo(vnd_str_t *vsp)
{
mblk_t *mp;
dl_info_req_t *dlir;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
BPRI_HI);
if (mp == NULL) {
vsp->vns_errno = VND_E_NOMEM;
return (1);
}
vsp->vns_state = VNS_S_INFO_SENT;
cv_broadcast(&vsp->vns_stcv);
mp->b_datap->db_type = M_PCPROTO;
dlir = (dl_info_req_t *)mp->b_rptr;
mp->b_wptr = (uchar_t *)&dlir[1];
dlir->dl_primitive = DL_INFO_REQ;
putnext(vsp->vns_wq, mp);
return (0);
}
static int
vnd_st_info(vnd_str_t *vsp)
{
dl_info_ack_t *dlia;
mblk_t *mp;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_inc_pop(vsp);
dlia = (dl_info_ack_t *)mp->b_rptr;
vsp->vns_dlpi_style = dlia->dl_provider_style;
vsp->vns_minwrite = dlia->dl_min_sdu;
vsp->vns_maxwrite = dlia->dl_max_sdu;
/*
* At this time we only support DL_ETHER devices.
*/
if (dlia->dl_mac_type != DL_ETHER) {
freemsg(mp);
vsp->vns_errno = VND_E_NOTETHER;
return (1);
}
/*
* Because vnd operates on entire packets, we need to manually account
* for the ethernet header information. We add the size of the
* ether_vlan_header to account for this, regardless if it is using
* vlans or not.
*/
vsp->vns_maxwrite += sizeof (struct ether_vlan_header);
freemsg(mp);
return (0);
}
static int
vnd_st_sexclusive(vnd_str_t *vsp)
{
mblk_t *mp;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_alloc(sizeof (dl_attach_req_t), DL_EXCLUSIVE_REQ);
if (mp == NULL) {
vsp->vns_errno = VND_E_NOMEM;
return (1);
}
vsp->vns_state = VNS_S_EXCLUSIVE_SENT;
cv_broadcast(&vsp->vns_stcv);
putnext(vsp->vns_wq, mp);
return (0);
}
static int
vnd_st_exclusive(vnd_str_t *vsp)
{
mblk_t *mp;
t_uscalar_t prim, cprim;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_inc_pop(vsp);
prim = ((dl_error_ack_t *)mp->b_rptr)->dl_primitive;
cprim = ((dl_ok_ack_t *)mp->b_rptr)->dl_correct_primitive;
if (prim != DL_OK_ACK && prim != DL_ERROR_ACK) {
vnd_drop_ctl(vsp, mp,
"wrong dlpi primitive for vnd_st_exclusive");
vsp->vns_errno = VND_E_DLPIINVAL;
return (1);
}
if (cprim != DL_EXCLUSIVE_REQ) {
vnd_drop_ctl(vsp, mp,
"vnd_st_exclusive: got ack/nack for wrong primitive");
vsp->vns_errno = VND_E_DLPIINVAL;
return (1);
}
if (prim == DL_ERROR_ACK)
vsp->vns_errno = VND_E_DLEXCL;
freemsg(mp);
return (prim == DL_ERROR_ACK);
}
/*
* Send down a DLPI_ATTACH_REQ.
*/
static int
vnd_st_sattach(vnd_str_t *vsp)
{
mblk_t *mp;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_alloc(sizeof (dl_attach_req_t), DL_ATTACH_REQ);
if (mp == NULL) {
vsp->vns_errno = VND_E_NOMEM;
return (1);
}
((dl_attach_req_t *)mp->b_rptr)->dl_ppa = 0;
vsp->vns_state = VNS_S_ATTACH_SENT;
cv_broadcast(&vsp->vns_stcv);
putnext(vsp->vns_wq, mp);
return (0);
}
static int
vnd_st_attach(vnd_str_t *vsp)
{
mblk_t *mp;
t_uscalar_t prim, cprim;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_inc_pop(vsp);
prim = ((dl_ok_ack_t *)mp->b_rptr)->dl_primitive;
cprim = ((dl_ok_ack_t *)mp->b_rptr)->dl_correct_primitive;
if (prim != DL_OK_ACK && prim != DL_ERROR_ACK) {
vnd_drop_ctl(vsp, mp, "vnd_st_attach: unknown primitive type");
vsp->vns_errno = VND_E_DLPIINVAL;
return (1);
}
if (cprim != DL_ATTACH_REQ) {
vnd_drop_ctl(vsp, mp,
"vnd_st_attach: Got ack/nack for wrong primitive");
vsp->vns_errno = VND_E_DLPIINVAL;
return (1);
}
if (prim == DL_ERROR_ACK)
vsp->vns_errno = VND_E_ATTACHFAIL;
freemsg(mp);
return (prim == DL_ERROR_ACK);
}
static int
vnd_st_sbind(vnd_str_t *vsp)
{
mblk_t *mp;
dl_bind_req_t *dbrp;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
DL_BIND_REQ);
if (mp == NULL) {
vsp->vns_errno = VND_E_NOMEM;
return (1);
}
dbrp = (dl_bind_req_t *)(mp->b_rptr);
dbrp->dl_sap = 0;
dbrp->dl_service_mode = DL_CLDLS;
vsp->vns_state = VNS_S_BIND_SENT;
cv_broadcast(&vsp->vns_stcv);
putnext(vsp->vns_wq, mp);
return (0);
}
static int
vnd_st_bind(vnd_str_t *vsp)
{
mblk_t *mp;
t_uscalar_t prim;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_inc_pop(vsp);
prim = ((dl_error_ack_t *)mp->b_rptr)->dl_primitive;
if (prim != DL_BIND_ACK && prim != DL_ERROR_ACK) {
vnd_drop_ctl(vsp, mp, "wrong dlpi primitive for vnd_st_bind");
vsp->vns_errno = VND_E_DLPIINVAL;
return (1);
}
if (prim == DL_ERROR_ACK)
vsp->vns_errno = VND_E_BINDFAIL;
freemsg(mp);
return (prim == DL_ERROR_ACK);
}
static int
vnd_st_spromisc(vnd_str_t *vsp, int type, vnd_str_state_t next)
{
mblk_t *mp;
dl_promiscon_req_t *dprp;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_alloc(sizeof (dl_promiscon_req_t), DL_PROMISCON_REQ);
if (mp == NULL) {
vsp->vns_errno = VND_E_NOMEM;
return (1);
}
dprp = (dl_promiscon_req_t *)mp->b_rptr;
dprp->dl_level = type;
vsp->vns_state = next;
cv_broadcast(&vsp->vns_stcv);
putnext(vsp->vns_wq, mp);
return (0);
}
static int
vnd_st_promisc(vnd_str_t *vsp)
{
mblk_t *mp;
t_uscalar_t prim, cprim;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_inc_pop(vsp);
prim = ((dl_ok_ack_t *)mp->b_rptr)->dl_primitive;
cprim = ((dl_ok_ack_t *)mp->b_rptr)->dl_correct_primitive;
if (prim != DL_OK_ACK && prim != DL_ERROR_ACK) {
vnd_drop_ctl(vsp, mp,
"wrong dlpi primitive for vnd_st_promisc");
vsp->vns_errno = VND_E_DLPIINVAL;
return (1);
}
if (cprim != DL_PROMISCON_REQ) {
vnd_drop_ctl(vsp, mp,
"vnd_st_promisc: Got ack/nack for wrong primitive");
vsp->vns_errno = VND_E_DLPIINVAL;
return (1);
}
if (prim == DL_ERROR_ACK)
vsp->vns_errno = VND_E_PROMISCFAIL;
freemsg(mp);
return (prim == DL_ERROR_ACK);
}
static int
vnd_st_scapabq(vnd_str_t *vsp)
{
mblk_t *mp;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
if (mp == NULL) {
vsp->vns_errno = VND_E_NOMEM;
return (1);
}
vsp->vns_state = VNS_S_CAPAB_Q_SENT;
cv_broadcast(&vsp->vns_stcv);
putnext(vsp->vns_wq, mp);
return (0);
}
static void
vnd_mac_input(vnd_str_t *vsp, mac_resource_t *unused, mblk_t *mp_chain,
mac_header_info_t *mhip)
{
int signal = 0;
mblk_t *mp;
vnd_pnsd_t *nsp = vsp->vns_nsd;
ASSERT(vsp != NULL);
ASSERT(mp_chain != NULL);
for (mp = mp_chain; mp != NULL; mp = mp_chain) {
uint16_t vid;
mp_chain = mp->b_next;
mp->b_next = NULL;
/*
* If we were operating in a traditional dlpi context then we
* would have enabled DLIOCRAW and rather than the fast path we
* would come through dld_str_rx_raw. That function does two
* things that we have to consider doing ourselves. The first is
* that it adjusts the b_rptr back to account for dld bumping us
* past the mac header. It also tries to account for cases where
* mac provides an illusion of the mac header. Fortunately, dld
* only allows the fastpath when the media type is the same as
* the native type. Therefore all we have to do here is adjust
* the b_rptr.
*/
ASSERT(mp->b_rptr >= DB_BASE(mp) + mhip->mhi_hdrsize);
mp->b_rptr -= mhip->mhi_hdrsize;
vid = VLAN_ID(mhip->mhi_tci);
if (mhip->mhi_istagged && vid != VLAN_ID_NONE) {
bcopy(mp->b_rptr, mp->b_rptr + 4, 12);
mp->b_rptr += 4;
}
if (nsp->vpnd_hooked && vnd_hook(vsp, &mp, nsp->vpnd_neti_v4,
nsp->vpnd_event_in_v4, nsp->vpnd_token_in_v4,
nsp->vpnd_neti_v6, nsp->vpnd_event_in_v6,
nsp->vpnd_token_in_v6, vnd_drop_hook_in, vnd_drop_in) != 0)
continue;
VND_STAT_INC(vsp, vks_rpackets, 1);
VND_STAT_INC(vsp, vks_rbytes, msgsize(mp));
DTRACE_VND5(recv, mblk_t *, mp, void *, NULL, void *, NULL,
vnd_str_t *, vsp, mblk_t *, mp);
mutex_enter(&vsp->vns_dq_read.vdq_lock);
signal |= vnd_dq_push(&vsp->vns_dq_read, mp, B_FALSE,
vnd_drop_in);
mutex_exit(&vsp->vns_dq_read.vdq_lock);
}
if (signal != 0) {
cv_broadcast(&vsp->vns_dq_read.vdq_ready);
pollwakeup(&vsp->vns_dev->vdd_ph, POLLIN | POLLRDNORM);
}
}
static void
vnd_mac_flow_control_stat(vnd_str_t *vsp, hrtime_t diff)
{
VND_STAT_INC(vsp, vks_nmacflow, 1);
VND_STAT_INC(vsp, vks_tmacflow, diff);
if (diff >= VND_LATENCY_1MS)
VND_STAT_INC(vsp, vks_mac_flow_1ms, 1);
if (diff >= VND_LATENCY_10MS)
VND_STAT_INC(vsp, vks_mac_flow_10ms, 1);
if (diff >= VND_LATENCY_100MS)
VND_STAT_INC(vsp, vks_mac_flow_100ms, 1);
if (diff >= VND_LATENCY_1S)
VND_STAT_INC(vsp, vks_mac_flow_1s, 1);
if (diff >= VND_LATENCY_10S)
VND_STAT_INC(vsp, vks_mac_flow_10s, 1);
}
/*
* This is a callback from MAC that indicates that we are allowed to send
* packets again.
*/
static void
vnd_mac_flow_control(void *arg, vnd_mac_cookie_t cookie)
{
vnd_str_t *vsp = arg;
hrtime_t now, diff;
mutex_enter(&vsp->vns_lock);
now = gethrtime();
/*
* Check for the case that we beat vnd_squeue_tx_one to the punch.
* There's also an additional case here that we got notified because
* we're sharing a device that ran out of tx descriptors, even though it
* wasn't because of us.
*/
if (!(vsp->vns_flags & VNS_F_FLOW_CONTROLLED)) {
vsp->vns_fcupdate = now;
mutex_exit(&vsp->vns_lock);
return;
}
ASSERT(vsp->vns_flags & VNS_F_FLOW_CONTROLLED);
ASSERT(vsp->vns_caps.vsc_fc_cookie == cookie);
vsp->vns_flags &= ~VNS_F_FLOW_CONTROLLED;
vsp->vns_caps.vsc_fc_cookie = NULL;
diff = now - vsp->vns_fclatch;
vsp->vns_fclatch = 0;
DTRACE_VND3(flow__resumed, vnd_str_t *, vsp, uint64_t,
vsp->vns_dq_write.vdq_cur, uintptr_t, cookie);
/*
* If someone has asked to flush the squeue and thus inserted a barrier,
* than we shouldn't schedule a drain.
*/
if (!(vsp->vns_flags & (VNS_F_DRAIN_SCHEDULED | VNS_F_BARRIER))) {
vsp->vns_flags |= VNS_F_DRAIN_SCHEDULED;
gsqueue_enter_one(vsp->vns_squeue, &vsp->vns_drainblk,
vnd_squeue_tx_drain, vsp, GSQUEUE_FILL,
VND_SQUEUE_TAG_MAC_FLOW_CONTROL);
}
mutex_exit(&vsp->vns_lock);
}
static void
vnd_mac_enter(vnd_str_t *vsp, mac_perim_handle_t *mphp)
{
ASSERT(MUTEX_HELD(&vsp->vns_lock));
VERIFY(vsp->vns_caps.vsc_capab_f(vsp->vns_caps.vsc_capab_hdl,
DLD_CAPAB_PERIM, mphp, DLD_ENABLE) == 0);
}
static void
vnd_mac_exit(vnd_str_t *vsp, mac_perim_handle_t mph)
{
ASSERT(MUTEX_HELD(&vsp->vns_lock));
VERIFY(vsp->vns_caps.vsc_capab_f(vsp->vns_caps.vsc_capab_hdl,
DLD_CAPAB_PERIM, mph, DLD_DISABLE) == 0);
}
static int
vnd_dld_cap_enable(vnd_str_t *vsp, vnd_rx_t rxfunc)
{
int ret;
dld_capab_direct_t d;
mac_perim_handle_t mph;
vnd_str_capab_t *c = &vsp->vns_caps;
bzero(&d, sizeof (d));
d.di_rx_cf = (uintptr_t)rxfunc;
d.di_rx_ch = vsp;
d.di_flags = DI_DIRECT_RAW;
vnd_mac_enter(vsp, &mph);
/*
* If we're coming in here for a second pass, we need to make sure that
* we remove an existing flow control notification callback, otherwise
* we'll create a duplicate that will remain with garbage data.
*/
if (c->vsc_tx_fc_hdl != NULL) {
ASSERT(c->vsc_set_fcb_hdl != NULL);
(void) c->vsc_set_fcb_f(c->vsc_set_fcb_hdl, NULL,
c->vsc_tx_fc_hdl);
c->vsc_tx_fc_hdl = NULL;
}
if (vsp->vns_caps.vsc_capab_f(c->vsc_capab_hdl,
DLD_CAPAB_DIRECT, &d, DLD_ENABLE) == 0) {
c->vsc_tx_f = (vnd_dld_tx_t)d.di_tx_df;
c->vsc_tx_hdl = d.di_tx_dh;
c->vsc_set_fcb_f = (vnd_dld_set_fcb_t)d.di_tx_cb_df;
c->vsc_set_fcb_hdl = d.di_tx_cb_dh;
c->vsc_is_fc_f = (vnd_dld_is_fc_t)d.di_tx_fctl_df;
c->vsc_is_fc_hdl = d.di_tx_fctl_dh;
c->vsc_tx_fc_hdl = c->vsc_set_fcb_f(c->vsc_set_fcb_hdl,
vnd_mac_flow_control, vsp);
c->vsc_flags |= VNS_C_DIRECT;
ret = 0;
} else {
vsp->vns_errno = VND_E_DIRECTFAIL;
ret = 1;
}
vnd_mac_exit(vsp, mph);
return (ret);
}
static int
vnd_st_capabq(vnd_str_t *vsp)
{
mblk_t *mp;
dl_capability_ack_t *cap;
dl_capability_sub_t *subp;
dl_capab_hcksum_t *hck;
dl_capab_dld_t *dld;
unsigned char *rp;
int ret = 0;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
mp = vnd_dlpi_inc_pop(vsp);
rp = mp->b_rptr;
cap = (dl_capability_ack_t *)rp;
if (cap->dl_sub_length == 0)
goto done;
/* Don't try to process something too big */
if (sizeof (dl_capability_ack_t) + cap->dl_sub_length > MBLKL(mp)) {
VND_STAT_INC(vsp, vks_ndlpidrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
vsp->vns_errno = VND_E_CAPACKINVAL;
ret = 1;
goto done;
}
rp += cap->dl_sub_offset;
while (cap->dl_sub_length > 0) {
subp = (dl_capability_sub_t *)rp;
/* Sanity check something crazy from down below */
if (subp->dl_length + sizeof (dl_capability_sub_t) >
cap->dl_sub_length) {
VND_STAT_INC(vsp, vks_ndlpidrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
vsp->vns_errno = VND_E_SUBCAPINVAL;
ret = 1;
goto done;
}
switch (subp->dl_cap) {
case DL_CAPAB_HCKSUM:
hck = (dl_capab_hcksum_t *)(rp +
sizeof (dl_capability_sub_t));
if (hck->hcksum_version != HCKSUM_CURRENT_VERSION) {
vsp->vns_caps.vsc_flags |= VNS_C_HCKSUM_BADVERS;
break;
}
if (dlcapabcheckqid(&hck->hcksum_mid, vsp->vns_lrq) !=
B_TRUE) {
vsp->vns_errno = VND_E_CAPABPASS;
ret = 1;
goto done;
}
vsp->vns_caps.vsc_flags |= VNS_C_HCKSUM;
vsp->vns_caps.vsc_hcksum_opts = hck->hcksum_txflags;
break;
case DL_CAPAB_DLD:
dld = (dl_capab_dld_t *)(rp +
sizeof (dl_capability_sub_t));
if (dld->dld_version != DLD_CURRENT_VERSION) {
vsp->vns_errno = VND_E_DLDBADVERS;
ret = 1;
goto done;
}
if (dlcapabcheckqid(&dld->dld_mid, vsp->vns_lrq) !=
B_TRUE) {
vsp->vns_errno = VND_E_CAPABPASS;
ret = 1;
goto done;
}
vsp->vns_caps.vsc_flags |= VNS_C_DLD;
vsp->vns_caps.vsc_capab_f =
(vnd_dld_cap_t)dld->dld_capab;
vsp->vns_caps.vsc_capab_hdl =
(void *)dld->dld_capab_handle;
/*
* At this point in time, we have to set up a direct
* function that drops all input. This validates that
* we'll be able to set up direct input and that we can
* easily switch it earlier to the real data function
* when we've plumbed everything up.
*/
if (vnd_dld_cap_enable(vsp, vnd_mac_drop_input) != 0) {
/* vns_errno set by vnd_dld_cap_enable */
ret = 1;
goto done;
}
break;
default:
/* Ignore unsupported cap */
break;
}
rp += sizeof (dl_capability_sub_t) + subp->dl_length;
cap->dl_sub_length -= sizeof (dl_capability_sub_t) +
subp->dl_length;
}
done:
/* Make sure we enabled direct callbacks */
if (ret == 0 && !(vsp->vns_caps.vsc_flags & VNS_C_DIRECT)) {
vsp->vns_errno = VND_E_DIRECTNOTSUP;
ret = 1;
}
freemsg(mp);
return (ret);
}
static void
vnd_st_sonline(vnd_str_t *vsp)
{
VERIFY(MUTEX_HELD(&vsp->vns_lock));
vsp->vns_state = VNS_S_ONLINE;
cv_broadcast(&vsp->vns_stcv);
}
static void
vnd_st_shutdown(vnd_str_t *vsp)
{
mac_perim_handle_t mph;
vnd_str_capab_t *vsc = &vsp->vns_caps;
VERIFY(MUTEX_HELD(&vsp->vns_lock));
/*
* At this point in time we know that there is no one transmitting as
* our final reference has been torn down and that vnd_s_close inserted
* a barrier to validate that everything is flushed.
*/
if (vsc->vsc_flags & VNS_C_DIRECT) {
vnd_mac_enter(vsp, &mph);
vsc->vsc_flags &= ~VNS_C_DIRECT;
(void) vsc->vsc_set_fcb_f(vsc->vsc_set_fcb_hdl, NULL,
vsc->vsc_tx_fc_hdl);
vsc->vsc_tx_fc_hdl = NULL;
(void) vsc->vsc_capab_f(vsc->vsc_capab_hdl, DLD_CAPAB_DIRECT,
NULL, DLD_DISABLE);
vnd_mac_exit(vsp, mph);
}
/*
* We could send an unbind, but dld also does that for us. As we add
* more capabilities and the like, we should revisit this.
*/
vsp->vns_state = VNS_S_ZOMBIE;
cv_broadcast(&vsp->vns_stcv);
}
/*
* Perform state transitions. This is a one way shot down the flow chart
* described in the big theory statement.
*/
static void
vnd_str_state_transition(void *arg)
{
boolean_t died = B_FALSE;
vnd_str_t *vsp = arg;
mblk_t *mp;
mutex_enter(&vsp->vns_lock);
if (vsp->vns_dlpi_inc == NULL && (vsp->vns_state != VNS_S_INITIAL &&
vsp->vns_state != VNS_S_SHUTTING_DOWN)) {
mutex_exit(&vsp->vns_lock);
return;
}
DTRACE_PROBE2(vnd__state__transition, uintptr_t, vsp,
vnd_str_state_t, vsp->vns_state);
switch (vsp->vns_state) {
case VNS_S_INITIAL:
VERIFY(vsp->vns_dlpi_inc == NULL);
if (vnd_st_sinfo(vsp) != 0)
died = B_TRUE;
break;
case VNS_S_INFO_SENT:
VERIFY(vsp->vns_dlpi_inc != NULL);
if (vnd_st_info(vsp) == 0) {
if (vnd_st_sexclusive(vsp) != 0)
died = B_TRUE;
} else {
died = B_TRUE;
}
break;
case VNS_S_EXCLUSIVE_SENT:
VERIFY(vsp->vns_dlpi_inc != NULL);
if (vnd_st_exclusive(vsp) == 0) {
if (vsp->vns_dlpi_style == DL_STYLE2) {
if (vnd_st_sattach(vsp) != 0)
died = B_TRUE;
} else {
if (vnd_st_sbind(vsp) != 0)
died = B_TRUE;
}
} else {
died = B_TRUE;
}
break;
case VNS_S_ATTACH_SENT:
VERIFY(vsp->vns_dlpi_inc != NULL);
if (vnd_st_attach(vsp) == 0) {
if (vnd_st_sbind(vsp) != 0)
died = B_TRUE;
} else {
died = B_TRUE;
}
break;
case VNS_S_BIND_SENT:
VERIFY(vsp->vns_dlpi_inc != NULL);
if (vnd_st_bind(vsp) == 0) {
if (vnd_st_spromisc(vsp, DL_PROMISC_SAP,
VNS_S_SAP_PROMISC_SENT) != 0)
died = B_TRUE;
} else {
died = B_TRUE;
}
break;
case VNS_S_SAP_PROMISC_SENT:
VERIFY(vsp->vns_dlpi_inc != NULL);
if (vnd_st_promisc(vsp) == 0) {
if (vnd_st_spromisc(vsp, DL_PROMISC_MULTI,
VNS_S_MULTI_PROMISC_SENT) != 0)
died = B_TRUE;
} else {
died = B_TRUE;
}
break;
case VNS_S_MULTI_PROMISC_SENT:
VERIFY(vsp->vns_dlpi_inc != NULL);
if (vnd_st_promisc(vsp) == 0) {
if (vnd_st_spromisc(vsp, DL_PROMISC_RX_ONLY,
VNS_S_RX_ONLY_PROMISC_SENT) != 0)
died = B_TRUE;
} else {
died = B_TRUE;
}
break;
case VNS_S_RX_ONLY_PROMISC_SENT:
VERIFY(vsp->vns_dlpi_inc != NULL);
if (vnd_st_promisc(vsp) == 0) {
if (vnd_st_scapabq(vsp) != 0)
died = B_TRUE;
} else {
died = B_TRUE;
}
break;
case VNS_S_CAPAB_Q_SENT:
if (vnd_st_capabq(vsp) != 0)
died = B_TRUE;
else
vnd_st_sonline(vsp);
break;
case VNS_S_SHUTTING_DOWN:
vnd_st_shutdown(vsp);
break;
case VNS_S_ZOMBIE:
while ((mp = vnd_dlpi_inc_pop(vsp)) != NULL)
vnd_drop_ctl(vsp, mp, "vsp committed suicide");
break;
default:
panic("vnd_str_t entered an unknown state");
}
if (died == B_TRUE) {
ASSERT(vsp->vns_errno != VND_E_SUCCESS);
vsp->vns_laststate = vsp->vns_state;
vsp->vns_state = VNS_S_ZOMBIE;
cv_broadcast(&vsp->vns_stcv);
}
mutex_exit(&vsp->vns_lock);
}
static void
vnd_dlpi_taskq_dispatch(void *arg)
{
vnd_str_t *vsp = arg;
int run = 1;
while (run != 0) {
vnd_str_state_transition(vsp);
mutex_enter(&vsp->vns_lock);
if (vsp->vns_flags & VNS_F_CONDEMNED ||
vsp->vns_dlpi_inc == NULL) {
run = 0;
vsp->vns_flags &= ~VNS_F_TASKQ_DISPATCHED;
}
if (vsp->vns_flags & VNS_F_CONDEMNED)
cv_signal(&vsp->vns_cancelcv);
mutex_exit(&vsp->vns_lock);
}
}
static int
vnd_neti_getifname(net_handle_t neti, phy_if_t phy, char *buf, const size_t len)
{
return (-1);
}
static int
vnd_neti_getmtu(net_handle_t neti, phy_if_t phy, lif_if_t ifdata)
{
return (-1);
}
static int
vnd_neti_getptmue(net_handle_t neti)
{
return (-1);
}
static int
vnd_neti_getlifaddr(net_handle_t neti, phy_if_t phy, lif_if_t ifdata,
size_t nelem, net_ifaddr_t type[], void *storage)
{
return (-1);
}
static int
vnd_neti_getlifzone(net_handle_t neti, phy_if_t phy, lif_if_t ifdata,
zoneid_t *zid)
{
return (-1);
}
static int
vnd_neti_getlifflags(net_handle_t neti, phy_if_t phy, lif_if_t ifdata,
uint64_t *flags)
{
return (-1);
}
static phy_if_t
vnd_neti_phygetnext(net_handle_t neti, phy_if_t phy)
{
return (-1);
}
static phy_if_t
vnd_neti_phylookup(net_handle_t neti, const char *name)
{
return (-1);
}
static lif_if_t
vnd_neti_lifgetnext(net_handle_t neti, phy_if_t phy, lif_if_t ifdata)
{
return (-1);
}
static int
vnd_neti_inject(net_handle_t neti, inject_t style, net_inject_t *packet)
{
return (-1);
}
static phy_if_t
vnd_neti_route(net_handle_t neti, struct sockaddr *address,
struct sockaddr *next)
{
return ((phy_if_t)-1);
}
static int
vnd_neti_ispchksum(net_handle_t neti, mblk_t *mp)
{
return (-1);
}
static int
vnd_neti_isvchksum(net_handle_t neti, mblk_t *mp)
{
return (-1);
}
static net_protocol_t vnd_neti_info_v4 = {
NETINFO_VERSION,
NHF_VND_INET,
vnd_neti_getifname,
vnd_neti_getmtu,
vnd_neti_getptmue,
vnd_neti_getlifaddr,
vnd_neti_getlifzone,
vnd_neti_getlifflags,
vnd_neti_phygetnext,
vnd_neti_phylookup,
vnd_neti_lifgetnext,
vnd_neti_inject,
vnd_neti_route,
vnd_neti_ispchksum,
vnd_neti_isvchksum
};
static net_protocol_t vnd_neti_info_v6 = {
NETINFO_VERSION,
NHF_VND_INET6,
vnd_neti_getifname,
vnd_neti_getmtu,
vnd_neti_getptmue,
vnd_neti_getlifaddr,
vnd_neti_getlifzone,
vnd_neti_getlifflags,
vnd_neti_phygetnext,
vnd_neti_phylookup,
vnd_neti_lifgetnext,
vnd_neti_inject,
vnd_neti_route,
vnd_neti_ispchksum,
vnd_neti_isvchksum
};
static int
vnd_netinfo_init(vnd_pnsd_t *nsp)
{
nsp->vpnd_neti_v4 = net_protocol_register(nsp->vpnd_nsid,
&vnd_neti_info_v4);
ASSERT(nsp->vpnd_neti_v4 != NULL);
nsp->vpnd_neti_v6 = net_protocol_register(nsp->vpnd_nsid,
&vnd_neti_info_v6);
ASSERT(nsp->vpnd_neti_v6 != NULL);
nsp->vpnd_family_v4.hf_version = HOOK_VERSION;
nsp->vpnd_family_v4.hf_name = "vnd_inet";
if (net_family_register(nsp->vpnd_neti_v4, &nsp->vpnd_family_v4) != 0) {
net_protocol_unregister(nsp->vpnd_neti_v4);
net_protocol_unregister(nsp->vpnd_neti_v6);
cmn_err(CE_NOTE, "vnd_netinfo_init: net_family_register "
"failed for stack %d", nsp->vpnd_nsid);
return (1);
}
nsp->vpnd_family_v6.hf_version = HOOK_VERSION;
nsp->vpnd_family_v6.hf_name = "vnd_inet6";
if (net_family_register(nsp->vpnd_neti_v6, &nsp->vpnd_family_v6) != 0) {
net_family_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_family_v4);
net_protocol_unregister(nsp->vpnd_neti_v4);
net_protocol_unregister(nsp->vpnd_neti_v6);
cmn_err(CE_NOTE, "vnd_netinfo_init: net_family_register "
"failed for stack %d", nsp->vpnd_nsid);
return (1);
}
nsp->vpnd_event_in_v4.he_version = HOOK_VERSION;
nsp->vpnd_event_in_v4.he_name = NH_PHYSICAL_IN;
nsp->vpnd_event_in_v4.he_flags = 0;
nsp->vpnd_event_in_v4.he_interested = B_FALSE;
nsp->vpnd_token_in_v4 = net_event_register(nsp->vpnd_neti_v4,
&nsp->vpnd_event_in_v4);
if (nsp->vpnd_token_in_v4 == NULL) {
net_family_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_family_v4);
net_family_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_family_v6);
net_protocol_unregister(nsp->vpnd_neti_v4);
net_protocol_unregister(nsp->vpnd_neti_v6);
cmn_err(CE_NOTE, "vnd_netinfo_init: net_event_register "
"failed for stack %d", nsp->vpnd_nsid);
return (1);
}
nsp->vpnd_event_in_v6.he_version = HOOK_VERSION;
nsp->vpnd_event_in_v6.he_name = NH_PHYSICAL_IN;
nsp->vpnd_event_in_v6.he_flags = 0;
nsp->vpnd_event_in_v6.he_interested = B_FALSE;
nsp->vpnd_token_in_v6 = net_event_register(nsp->vpnd_neti_v6,
&nsp->vpnd_event_in_v6);
if (nsp->vpnd_token_in_v6 == NULL) {
net_event_shutdown(nsp->vpnd_neti_v4, &nsp->vpnd_event_in_v4);
net_event_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_event_in_v4);
net_family_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_family_v4);
net_family_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_family_v6);
net_protocol_unregister(nsp->vpnd_neti_v4);
net_protocol_unregister(nsp->vpnd_neti_v6);
cmn_err(CE_NOTE, "vnd_netinfo_init: net_event_register "
"failed for stack %d", nsp->vpnd_nsid);
return (1);
}
nsp->vpnd_event_out_v4.he_version = HOOK_VERSION;
nsp->vpnd_event_out_v4.he_name = NH_PHYSICAL_OUT;
nsp->vpnd_event_out_v4.he_flags = 0;
nsp->vpnd_event_out_v4.he_interested = B_FALSE;
nsp->vpnd_token_out_v4 = net_event_register(nsp->vpnd_neti_v4,
&nsp->vpnd_event_out_v4);
if (nsp->vpnd_token_out_v4 == NULL) {
net_event_shutdown(nsp->vpnd_neti_v6, &nsp->vpnd_event_in_v6);
net_event_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_event_in_v6);
net_event_shutdown(nsp->vpnd_neti_v4, &nsp->vpnd_event_in_v4);
net_event_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_event_in_v4);
net_family_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_family_v4);
net_family_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_family_v6);
net_protocol_unregister(nsp->vpnd_neti_v4);
net_protocol_unregister(nsp->vpnd_neti_v6);
cmn_err(CE_NOTE, "vnd_netinfo_init: net_event_register "
"failed for stack %d", nsp->vpnd_nsid);
return (1);
}
nsp->vpnd_event_out_v6.he_version = HOOK_VERSION;
nsp->vpnd_event_out_v6.he_name = NH_PHYSICAL_OUT;
nsp->vpnd_event_out_v6.he_flags = 0;
nsp->vpnd_event_out_v6.he_interested = B_FALSE;
nsp->vpnd_token_out_v6 = net_event_register(nsp->vpnd_neti_v6,
&nsp->vpnd_event_out_v6);
if (nsp->vpnd_token_out_v6 == NULL) {
net_event_shutdown(nsp->vpnd_neti_v6, &nsp->vpnd_event_in_v6);
net_event_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_event_in_v6);
net_event_shutdown(nsp->vpnd_neti_v6, &nsp->vpnd_event_in_v6);
net_event_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_event_in_v6);
net_event_shutdown(nsp->vpnd_neti_v4, &nsp->vpnd_event_in_v4);
net_event_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_event_in_v4);
net_family_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_family_v4);
net_family_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_family_v6);
net_protocol_unregister(nsp->vpnd_neti_v4);
net_protocol_unregister(nsp->vpnd_neti_v6);
cmn_err(CE_NOTE, "vnd_netinfo_init: net_event_register "
"failed for stack %d", nsp->vpnd_nsid);
return (1);
}
return (0);
}
static void
vnd_netinfo_shutdown(vnd_pnsd_t *nsp)
{
int ret;
ret = net_event_shutdown(nsp->vpnd_neti_v4, &nsp->vpnd_event_in_v4);
VERIFY(ret == 0);
ret = net_event_shutdown(nsp->vpnd_neti_v4, &nsp->vpnd_event_out_v4);
VERIFY(ret == 0);
ret = net_event_shutdown(nsp->vpnd_neti_v6, &nsp->vpnd_event_in_v6);
VERIFY(ret == 0);
ret = net_event_shutdown(nsp->vpnd_neti_v6, &nsp->vpnd_event_out_v6);
VERIFY(ret == 0);
}
static void
vnd_netinfo_fini(vnd_pnsd_t *nsp)
{
int ret;
ret = net_event_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_event_in_v4);
VERIFY(ret == 0);
ret = net_event_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_event_out_v4);
VERIFY(ret == 0);
ret = net_event_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_event_in_v6);
VERIFY(ret == 0);
ret = net_event_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_event_out_v6);
VERIFY(ret == 0);
ret = net_family_unregister(nsp->vpnd_neti_v4, &nsp->vpnd_family_v4);
VERIFY(ret == 0);
ret = net_family_unregister(nsp->vpnd_neti_v6, &nsp->vpnd_family_v6);
VERIFY(ret == 0);
ret = net_protocol_unregister(nsp->vpnd_neti_v4);
VERIFY(ret == 0);
ret = net_protocol_unregister(nsp->vpnd_neti_v6);
VERIFY(ret == 0);
}
static void
vnd_strbarrier_cb(void *arg, mblk_t *bmp, gsqueue_t *gsp, void *dummy)
{
vnd_str_t *vsp = arg;
VERIFY(bmp == &vsp->vns_barrierblk);
mutex_enter(&vsp->vns_lock);
VERIFY(vsp->vns_flags & VNS_F_BARRIER);
VERIFY(!(vsp->vns_flags & VNS_F_BARRIER_DONE));
vsp->vns_flags |= VNS_F_BARRIER_DONE;
mutex_exit(&vsp->vns_lock);
/*
* For better or worse, we have to broadcast here as we could have a
* thread that's blocked for completion as well as one that's blocked
* waiting to do a barrier itself.
*/
cv_broadcast(&vsp->vns_barriercv);
}
/*
* This is a data barrier for the stream while it is in fastpath mode. It blocks
* and ensures that there is nothing else in the squeue.
*/
static void
vnd_strbarrier(vnd_str_t *vsp)
{
mutex_enter(&vsp->vns_lock);
while (vsp->vns_flags & VNS_F_BARRIER)
cv_wait(&vsp->vns_barriercv, &vsp->vns_lock);
vsp->vns_flags |= VNS_F_BARRIER;
mutex_exit(&vsp->vns_lock);
gsqueue_enter_one(vsp->vns_squeue, &vsp->vns_barrierblk,
vnd_strbarrier_cb, vsp, GSQUEUE_PROCESS, VND_SQUEUE_TAG_STRBARRIER);
mutex_enter(&vsp->vns_lock);
while (!(vsp->vns_flags & VNS_F_BARRIER_DONE))
cv_wait(&vsp->vns_barriercv, &vsp->vns_lock);
vsp->vns_flags &= ~VNS_F_BARRIER;
vsp->vns_flags &= ~VNS_F_BARRIER_DONE;
mutex_exit(&vsp->vns_lock);
/*
* We have to broadcast in case anyone is waiting for the barrier
* themselves.
*/
cv_broadcast(&vsp->vns_barriercv);
}
/*
* Based on the type of message that we're dealing with we're going to want to
* do one of several things. Basically if it looks like it's something we know
* about, we should probably handle it in one of our transition threads.
* Otherwise, we should just simply putnext.
*/
static int
vnd_s_rput(queue_t *q, mblk_t *mp)
{
t_uscalar_t prim;
int dispatch = 0;
vnd_str_t *vsp = q->q_ptr;
switch (DB_TYPE(mp)) {
case M_PROTO:
case M_PCPROTO:
if (MBLKL(mp) < sizeof (t_uscalar_t)) {
vnd_drop_ctl(vsp, mp, "PROTO message too short");
break;
}
prim = ((union DL_primitives *)mp->b_rptr)->dl_primitive;
if (prim == DL_UNITDATA_REQ || prim == DL_UNITDATA_IND) {
vnd_drop_ctl(vsp, mp,
"recieved an unsupported dlpi DATA req");
break;
}
/*
* Enqueue the entry and fire off a taskq dispatch.
*/
mutex_enter(&vsp->vns_lock);
vnd_dlpi_inc_push(vsp, mp);
if (!(vsp->vns_flags & VNS_F_TASKQ_DISPATCHED)) {
dispatch = 1;
vsp->vns_flags |= VNS_F_TASKQ_DISPATCHED;
}
mutex_exit(&vsp->vns_lock);
if (dispatch != 0)
taskq_dispatch_ent(vnd_taskq, vnd_dlpi_taskq_dispatch,
vsp, 0, &vsp->vns_tqe);
break;
case M_DATA:
vnd_drop_in(vsp, mp, "M_DATA via put(9E)");
break;
default:
putnext(vsp->vns_rq, mp);
}
return (0);
}
static void
vnd_strioctl(queue_t *q, vnd_str_t *vsp, mblk_t *mp, struct iocblk *iocp)
{
int error;
vnd_strioc_t *visp;
if (iocp->ioc_cmd != VND_STRIOC_ASSOCIATE ||
iocp->ioc_count != TRANSPARENT) {
error = EINVAL;
goto nak;
}
/*
* All streams ioctls that we support must use kcred as a means to
* distinguish that this is a layered open by the kernel as opposed to
* one by a user who has done an I_PUSH of the module.
*/
if (iocp->ioc_cr != kcred) {
error = EPERM;
goto nak;
}
if (mp->b_cont == NULL) {
error = EAGAIN;
goto nak;
}
visp = kmem_alloc(sizeof (vnd_strioc_t), KM_SLEEP);
ASSERT(MBLKL(mp->b_cont) == sizeof (caddr_t));
visp->vs_addr = *(caddr_t *)mp->b_cont->b_rptr;
visp->vs_state = VSS_COPYIN;
mcopyin(mp, (void *)visp, sizeof (vnd_strioc_associate_t), NULL);
qreply(q, mp);
return;
nak:
if (mp->b_cont != NULL) {
freemsg(mp->b_cont);
mp->b_cont = NULL;
}
iocp->ioc_error = error;
mp->b_datap->db_type = M_IOCNAK;
iocp->ioc_count = 0;
qreply(q, mp);
}
static void
vnd_striocdata(queue_t *q, vnd_str_t *vsp, mblk_t *mp, struct copyresp *csp)
{
int error;
vnd_str_state_t state;
struct copyreq *crp;
vnd_strioc_associate_t *vss;
vnd_dev_t *vdp = NULL;
vnd_pnsd_t *nsp = NULL;
char iname[2*VND_NAMELEN];
zone_t *zone;
vnd_strioc_t *visp;
visp = (vnd_strioc_t *)csp->cp_private;
/* If it's not ours, it's not our problem */
if (csp->cp_cmd != VND_STRIOC_ASSOCIATE) {
if (q->q_next != NULL) {
putnext(q, mp);
} else {
VND_STAT_INC(vsp, vks_ndlpidrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
vnd_drop_ctl(vsp, mp, "uknown cmd for M_IOCDATA");
}
kmem_free(visp, sizeof (vnd_strioc_t));
return;
}
/* The nak is already sent for us */
if (csp->cp_rval != 0) {
vnd_drop_ctl(vsp, mp, "M_COPYIN failed");
kmem_free(visp, sizeof (vnd_strioc_t));
return;
}
/* Data is sitting for us in b_cont */
if (mp->b_cont == NULL ||
MBLKL(mp->b_cont) != sizeof (vnd_strioc_associate_t)) {
kmem_free(visp, sizeof (vnd_strioc_t));
miocnak(q, mp, 0, EINVAL);
qreply(q, mp);
return;
}
vss = (vnd_strioc_associate_t *)mp->b_cont->b_rptr;
vdp = vnd_dev_lookup(vss->vsa_minor);
if (vdp == NULL) {
error = EIO;
vss->vsa_errno = VND_E_NODEV;
goto nak;
}
nsp = vnd_nsd_lookup(vss->vsa_nsid);
if (nsp == NULL) {
error = EIO;
vss->vsa_errno = VND_E_NONETSTACK;
goto nak;
}
mutex_enter(&vsp->vns_lock);
if (!(vsp->vns_flags & VNS_F_NEED_ZONE)) {
mutex_exit(&vsp->vns_lock);
error = EEXIST;
vss->vsa_errno = VND_E_ASSOCIATED;
goto nak;
}
vsp->vns_nsd = nsp;
vsp->vns_flags &= ~VNS_F_NEED_ZONE;
vsp->vns_flags |= VNS_F_TASKQ_DISPATCHED;
mutex_exit(&vsp->vns_lock);
taskq_dispatch_ent(vnd_taskq, vnd_dlpi_taskq_dispatch, vsp, 0,
&vsp->vns_tqe);
/* At this point we need to wait until we have transitioned to ONLINE */
mutex_enter(&vsp->vns_lock);
while (vsp->vns_state != VNS_S_ONLINE && vsp->vns_state != VNS_S_ZOMBIE)
cv_wait(&vsp->vns_stcv, &vsp->vns_lock);
state = vsp->vns_state;
mutex_exit(&vsp->vns_lock);
if (state == VNS_S_ZOMBIE) {
vss->vsa_errno = vsp->vns_errno;
error = EIO;
goto nak;
}
mutex_enter(&vdp->vdd_lock);
mutex_enter(&vsp->vns_lock);
VERIFY(vdp->vdd_str == NULL);
/*
* Now initialize the remaining kstat properties and let's go ahead and
* create it.
*/
(void) snprintf(iname, sizeof (iname), "z%d_%d",
vdp->vdd_nsd->vpnd_zid, vdp->vdd_minor);
vsp->vns_kstat = kstat_create_zone("vnd", vdp->vdd_minor, iname, "net",
KSTAT_TYPE_NAMED, sizeof (vnd_str_stat_t) / sizeof (kstat_named_t),
KSTAT_FLAG_VIRTUAL, GLOBAL_ZONEID);
if (vsp->vns_kstat == NULL) {
error = EIO;
vss->vsa_errno = VND_E_KSTATCREATE;
mutex_exit(&vsp->vns_lock);
mutex_exit(&vdp->vdd_lock);
goto nak;
}
vdp->vdd_str = vsp;
vsp->vns_dev = vdp;
/*
* Now, it's time to do the las thing that can fail, changing out the
* input function. After this we know that we can receive data, so we
* should make sure that we're ready.
*/
if (vnd_dld_cap_enable(vsp, vnd_mac_input) != 0) {
error = EIO;
vss->vsa_errno = VND_E_DIRECTFAIL;
vdp->vdd_str = NULL;
vsp->vns_dev = NULL;
mutex_exit(&vsp->vns_lock);
mutex_exit(&vdp->vdd_lock);
goto nak;
}
zone = zone_find_by_id(vdp->vdd_nsd->vpnd_zid);
ASSERT(zone != NULL);
vsp->vns_kstat->ks_data = &vsp->vns_ksdata;
/* Account for zone name */
vsp->vns_kstat->ks_data_size += strlen(zone->zone_name) + 1;
/* Account for eventual link name */
vsp->vns_kstat->ks_data_size += VND_NAMELEN;
kstat_named_setstr(&vsp->vns_ksdata.vks_zonename, zone->zone_name);
kstat_named_setstr(&vdp->vdd_str->vns_ksdata.vks_linkname,
vdp->vdd_lname);
zone_rele(zone);
kstat_install(vsp->vns_kstat);
mutex_exit(&vsp->vns_lock);
mutex_exit(&vdp->vdd_lock);
/*
* Note that the vnd_str_t does not keep a permanent hold on the
* vnd_pnsd_t. We leave that up to the vnd_dev_t as that's also what
* the nestack goes through to take care of everything.
*/
vss->vsa_errno = VND_E_SUCCESS;
nak:
if (vdp != NULL)
vnd_dev_rele(vdp);
if (nsp != NULL)
vnd_nsd_rele(nsp);
/*
* Change the copyin request to a copyout. Note that we can't use
* mcopyout here as it only works when the DB_TYPE is M_IOCTL. That's
* okay, as the copyin vs. copyout is basically the same.
*/
DB_TYPE(mp) = M_COPYOUT;
visp->vs_state = VSS_COPYOUT;
crp = (struct copyreq *)mp->b_rptr;
crp->cq_private = (void *)visp;
crp->cq_addr = visp->vs_addr;
crp->cq_size = sizeof (vnd_strioc_associate_t);
qreply(q, mp);
}
static void
vnd_stroutdata(queue_t *q, vnd_str_t *vsp, mblk_t *mp, struct copyresp *csp)
{
ASSERT(csp->cp_private != NULL);
kmem_free(csp->cp_private, sizeof (vnd_strioc_t));
if (csp->cp_cmd != VND_STRIOC_ASSOCIATE) {
if (q->q_next != NULL) {
putnext(q, mp);
} else {
VND_STAT_INC(vsp, vks_ndlpidrops, 1);
VND_STAT_INC(vsp, vks_tdrops, 1);
vnd_drop_ctl(vsp, mp, "uknown cmd for M_IOCDATA");
}
return;
}
/* The nak is already sent for us */
if (csp->cp_rval != 0) {
vnd_drop_ctl(vsp, mp, "M_COPYOUT failed");
return;
}
/* Ack and let's be done with it all */
miocack(q, mp, 0, 0);
}
static int
vnd_s_wput(queue_t *q, mblk_t *mp)
{
vnd_str_t *vsp = q->q_ptr;
struct copyresp *crp;
vnd_strioc_state_t vstate;
vnd_strioc_t *visp;
switch (DB_TYPE(mp)) {
case M_IOCTL:
vnd_strioctl(q, vsp, mp, (struct iocblk *)mp->b_rptr);
return (0);
case M_IOCDATA:
crp = (struct copyresp *)mp->b_rptr;
ASSERT(crp->cp_private != NULL);
visp = (vnd_strioc_t *)crp->cp_private;
vstate = visp->vs_state;
ASSERT(vstate == VSS_COPYIN || vstate == VSS_COPYOUT);
if (vstate == VSS_COPYIN)
vnd_striocdata(q, vsp, mp,
(struct copyresp *)mp->b_rptr);
else
vnd_stroutdata(q, vsp, mp,
(struct copyresp *)mp->b_rptr);
return (0);
default:
break;
}
if (q->q_next != NULL)
putnext(q, mp);
else
vnd_drop_ctl(vsp, mp, "!M_IOCTL in wput");
return (0);
}
static int
vnd_s_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
{
vnd_str_t *vsp;
uint_t rand;
if (q->q_ptr != NULL)
return (EINVAL);
if (!(sflag & MODOPEN))
return (ENXIO);
if (credp != kcred)
return (EPERM);
vsp = kmem_cache_alloc(vnd_str_cache, KM_SLEEP);
bzero(vsp, sizeof (*vsp));
mutex_init(&vsp->vns_lock, NULL, MUTEX_DRIVER, NULL);
cv_init(&vsp->vns_cancelcv, NULL, CV_DRIVER, NULL);
cv_init(&vsp->vns_barriercv, NULL, CV_DRIVER, NULL);
cv_init(&vsp->vns_stcv, NULL, CV_DRIVER, NULL);
vsp->vns_state = VNS_S_INITIAL;
mutex_init(&vsp->vns_dq_read.vdq_lock, NULL, MUTEX_DRIVER, NULL);
mutex_init(&vsp->vns_dq_write.vdq_lock, NULL, MUTEX_DRIVER, NULL);
mutex_enter(&vnd_dev_lock);
vsp->vns_dq_read.vdq_max = vnd_vdq_default_size;
vsp->vns_dq_read.vdq_vns = vsp;
vsp->vns_dq_write.vdq_max = vnd_vdq_default_size;
vsp->vns_dq_write.vdq_vns = vsp;
mutex_exit(&vnd_dev_lock);
vsp->vns_rq = q;
vsp->vns_wq = WR(q);
q->q_ptr = WR(q)->q_ptr = vsp;
vsp->vns_flags = VNS_F_NEED_ZONE;
vsp->vns_nflush = vnd_flush_nburst;
vsp->vns_bsize = vnd_flush_burst_size;
(void) random_get_pseudo_bytes((uint8_t *)&rand, sizeof (rand));
vsp->vns_squeue = gsqueue_set_get(vnd_sqset, rand);
/*
* We create our kstat and initialize all of its fields now, but we
* don't install it until we actually do the zone association so we can
* get everything.
*/
kstat_named_init(&vsp->vns_ksdata.vks_rbytes, "rbytes",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_rpackets, "rpackets",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_obytes, "obytes",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_opackets, "opackets",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_nhookindrops, "nhookindrops",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_nhookoutdrops, "nhookoutdrops",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_ndlpidrops, "ndlpidrops",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_ndataindrops, "ndataindrops",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_ndataoutdrops, "ndataoutdrops",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_tdrops, "total_drops",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_linkname, "linkname",
KSTAT_DATA_STRING);
kstat_named_init(&vsp->vns_ksdata.vks_zonename, "zonename",
KSTAT_DATA_STRING);
kstat_named_init(&vsp->vns_ksdata.vks_nmacflow, "flowcontrol_events",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_tmacflow, "flowcontrol_time",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_mac_flow_1ms, "flowcontrol_1ms",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_mac_flow_10ms, "flowcontrol_10ms",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_mac_flow_100ms,
"flowcontrol_100ms", KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_mac_flow_1s, "flowcontrol_1s",
KSTAT_DATA_UINT64);
kstat_named_init(&vsp->vns_ksdata.vks_mac_flow_10s, "flowcontrol_10s",
KSTAT_DATA_UINT64);
qprocson(q);
/*
* Now that we've called qprocson, grab the lower module for making sure
* that we don't have any pass through modules.
*/
vsp->vns_lrq = RD(vsp->vns_wq->q_next);
return (0);
}
static int
vnd_s_close(queue_t *q, int flag, cred_t *credp)
{
vnd_str_t *vsp;
mblk_t *mp;
VERIFY(WR(q)->q_next != NULL);
vsp = q->q_ptr;
ASSERT(vsp != NULL);
/*
* We need to transition ourselves down. This means that we have a few
* important different things to do in the process of tearing down our
* input and output buffers, making sure we've drained the current
* squeue, and disabling the fast path. Before we disable the fast path,
* we should make sure the squeue is drained. Because we're in streams
* close, we know that no packets can come into us from userland, but we
* can receive more. As such, the following is the exact order of things
* that we do:
*
* 1) flush the vns_dq_read
* 2) Insert the drain mblk
* 3) When it's been received, tear down the fast path by kicking
* off the state machine.
* 4) One final flush of both the vns_dq_read,vns_dq_write
*/
vnd_dq_flush(&vsp->vns_dq_read, vnd_drop_in);
vnd_strbarrier(vsp);
mutex_enter(&vsp->vns_lock);
vsp->vns_state = VNS_S_SHUTTING_DOWN;
if (!(vsp->vns_flags & VNS_F_TASKQ_DISPATCHED)) {
vsp->vns_flags |= VNS_F_TASKQ_DISPATCHED;
taskq_dispatch_ent(vnd_taskq, vnd_dlpi_taskq_dispatch, vsp,
0, &vsp->vns_tqe);
}
while (vsp->vns_state != VNS_S_ZOMBIE)
cv_wait(&vsp->vns_stcv, &vsp->vns_lock);
mutex_exit(&vsp->vns_lock);
qprocsoff(q);
mutex_enter(&vsp->vns_lock);
vsp->vns_flags |= VNS_F_CONDEMNED;
while (vsp->vns_flags & VNS_F_TASKQ_DISPATCHED)
cv_wait(&vsp->vns_cancelcv, &vsp->vns_lock);
while ((mp = vnd_dlpi_inc_pop(vsp)) != NULL)
vnd_drop_ctl(vsp, mp, "vnd_s_close");
mutex_exit(&vsp->vns_lock);
q->q_ptr = NULL;
vnd_dq_flush(&vsp->vns_dq_read, vnd_drop_in);
vnd_dq_flush(&vsp->vns_dq_write, vnd_drop_out);
mutex_destroy(&vsp->vns_dq_read.vdq_lock);
mutex_destroy(&vsp->vns_dq_write.vdq_lock);
if (vsp->vns_kstat != NULL)
kstat_delete(vsp->vns_kstat);
mutex_destroy(&vsp->vns_lock);
cv_destroy(&vsp->vns_stcv);
cv_destroy(&vsp->vns_barriercv);
cv_destroy(&vsp->vns_cancelcv);
kmem_cache_free(vnd_str_cache, vsp);
return (0);
}
static vnd_mac_cookie_t
vnd_squeue_tx_one(vnd_str_t *vsp, mblk_t *mp)
{
hrtime_t txtime;
vnd_mac_cookie_t vc;
VND_STAT_INC(vsp, vks_opackets, 1);
VND_STAT_INC(vsp, vks_obytes, msgsize(mp));
DTRACE_VND5(send, mblk_t *, mp, void *, NULL, void *, NULL,
vnd_str_t *, vsp, mblk_t *, mp);
/* Actually tx now */
txtime = gethrtime();
vc = vsp->vns_caps.vsc_tx_f(vsp->vns_caps.vsc_tx_hdl,
mp, 0, MAC_DROP_ON_NO_DESC);
/*
* We need to check two different conditions before we immediately set
* the flow control lock. The first thing that we need to do is verify
* that this is an instance of hard flow control, so to say. The flow
* control callbacks won't always fire in cases where we still get a
* cookie returned. The explicit check for flow control will guarantee
* us that we'll get a subsequent notification callback.
*
* The second case comes about because we do not hold the
* vnd_str_t`vns_lock across calls to tx, we need to determine if a flow
* control notification already came across for us in a different thread
* calling vnd_mac_flow_control(). To deal with this, we record a
* timestamp every time that we change the flow control state. We grab
* txtime here before we transmit because that guarantees that the
* hrtime_t of the call to vnd_mac_flow_control() will be after txtime.
*
* If the flow control notification beat us to the punch, the value of
* vns_fcupdate will be larger than the value of txtime, and we should
* just record the statistics. However, if we didn't beat it to the
* punch (txtime > vns_fcupdate), then we know that it's safe to wait
* for a notification.
*/
if (vc != NULL) {
hrtime_t diff;
if (vsp->vns_caps.vsc_is_fc_f(vsp->vns_caps.vsc_is_fc_hdl,
vc) == 0)
return (NULL);
mutex_enter(&vsp->vns_lock);
diff = vsp->vns_fcupdate - txtime;
if (diff > 0) {
mutex_exit(&vsp->vns_lock);
vnd_mac_flow_control_stat(vsp, diff);
return (NULL);
}
vsp->vns_flags |= VNS_F_FLOW_CONTROLLED;
vsp->vns_caps.vsc_fc_cookie = vc;
vsp->vns_fclatch = txtime;
vsp->vns_fcupdate = txtime;
DTRACE_VND3(flow__blocked, vnd_str_t *, vsp,
uint64_t, vsp->vns_dq_write.vdq_cur, uintptr_t, vc);
mutex_exit(&vsp->vns_lock);
}
return (vc);
}
static void
vnd_squeue_tx_drain(void *arg, mblk_t *drain_mp, gsqueue_t *gsp, void *dummy)
{
mblk_t *mp;
int nmps;
size_t mptot, nflush, bsize;
boolean_t blocked, empty;
vnd_data_queue_t *vqp;
vnd_str_t *vsp = arg;
mutex_enter(&vsp->vns_lock);
/*
* We either enter here via an squeue or via vnd_squeue_tx_append(). In
* the former case we need to mark that there is no longer an active
* user of the drain block.
*/
if (drain_mp != NULL) {
VERIFY(drain_mp == &vsp->vns_drainblk);
VERIFY(vsp->vns_flags & VNS_F_DRAIN_SCHEDULED);
vsp->vns_flags &= ~VNS_F_DRAIN_SCHEDULED;
}
/*
* If we're still flow controlled or under a flush barrier, nothing to
* do.
*/
if (vsp->vns_flags & (VNS_F_FLOW_CONTROLLED | VNS_F_BARRIER)) {
mutex_exit(&vsp->vns_lock);
return;
}
nflush = vsp->vns_nflush;
bsize = vsp->vns_bsize;
mutex_exit(&vsp->vns_lock);
nmps = 0;
mptot = 0;
blocked = B_FALSE;
vqp = &vsp->vns_dq_write;
while (nmps < nflush && mptot <= bsize) {
mutex_enter(&vqp->vdq_lock);
if (vnd_dq_pop(vqp, &mp) == 0) {
mutex_exit(&vqp->vdq_lock);
break;
}
mutex_exit(&vqp->vdq_lock);
nmps++;
mptot += msgsize(mp);
if (vnd_squeue_tx_one(vsp, mp) != NULL) {
blocked = B_TRUE;
break;
}
}
empty = vnd_dq_is_empty(&vsp->vns_dq_write);
/*
* If the queue is not empty, we're not blocked, and there isn't a drain
* scheduled, put it into the squeue with the drain block and
* GSQUEUE_FILL.
*/
if (blocked == B_FALSE && empty == B_FALSE) {
mutex_enter(&vsp->vns_lock);
if (!(vsp->vns_flags & VNS_F_DRAIN_SCHEDULED)) {
mblk_t *mp = &vsp->vns_drainblk;
vsp->vns_flags |= VNS_F_DRAIN_SCHEDULED;
gsqueue_enter_one(vsp->vns_squeue,
mp, vnd_squeue_tx_drain, vsp,
GSQUEUE_FILL, VND_SQUEUE_TAG_TX_DRAIN);
}
mutex_exit(&vsp->vns_lock);
}
/*
* If we drained some amount of data, we need to signal the data queue.
*/
if (nmps > 0) {
cv_broadcast(&vsp->vns_dq_write.vdq_ready);
pollwakeup(&vsp->vns_dev->vdd_ph, POLLOUT);
}
}
static void
vnd_squeue_tx_append(void *arg, mblk_t *mp, gsqueue_t *gsp, void *dummy)
{
vnd_str_t *vsp = arg;
vnd_data_queue_t *vqp;
vnd_pnsd_t *nsp = vsp->vns_nsd;
/*
* Before we append this packet, we should run it through the firewall
* rules.
*/
if (nsp->vpnd_hooked && vnd_hook(vsp, &mp, nsp->vpnd_neti_v4,
nsp->vpnd_event_out_v4, nsp->vpnd_token_out_v4, nsp->vpnd_neti_v6,
nsp->vpnd_event_out_v6, nsp->vpnd_token_out_v6, vnd_drop_hook_out,
vnd_drop_out) != 0)
return;
/*
* We earlier reserved space for this packet. So for now simply append
* it and call drain. We know that no other drain can be going on right
* now thanks to the squeue.
*/
vqp = &vsp->vns_dq_write;
mutex_enter(&vqp->vdq_lock);
(void) vnd_dq_push(&vsp->vns_dq_write, mp, B_TRUE, vnd_drop_panic);
mutex_exit(&vqp->vdq_lock);
vnd_squeue_tx_drain(vsp, NULL, NULL, NULL);
}
/*
* We need to see if this is a valid name of sorts for us. That means a few
* things. First off, we can't assume that what we've been given has actually
* been null terminated. More importantly, that it's a valid name as far as
* ddi_create_minor_node is concerned (that means no '@', '/', or ' '). We
* further constrain ourselves to simply alphanumeric characters and a few
* additional ones, ':', '-', and '_'.
*/
static int
vnd_validate_name(const char *buf, size_t buflen)
{
int i, len;
/* First make sure a null terminator exists */
for (i = 0; i < buflen; i++)
if (buf[i] == '\0')
break;
len = i;
if (i == 0 || i == buflen)
return (0);
for (i = 0; i < len; i++)
if (!isalnum(buf[i]) && buf[i] != ':' && buf[i] != '-' &&
buf[i] != '_')
return (0);
return (1);
}
static int
vnd_ioctl_attach(vnd_dev_t *vdp, uintptr_t arg, cred_t *credp, int cpflag)
{
vnd_ioc_attach_t via;
vnd_strioc_associate_t vss;
vnd_pnsd_t *nsp;
zone_t *zonep;
zoneid_t zid;
char buf[2*VND_NAMELEN];
int ret, rp;
if (secpolicy_net_config(credp, B_FALSE) != 0)
return (EPERM);
if (secpolicy_net_rawaccess(credp) != 0)
return (EPERM);
if (ddi_copyin((void *)arg, &via, sizeof (via), cpflag) != 0)
return (EFAULT);
via.via_errno = VND_E_SUCCESS;
if (vnd_validate_name(via.via_name, VND_NAMELEN) == 0) {
via.via_errno = VND_E_BADNAME;
ret = EIO;
goto errcopyout;
}
/*
* Only the global zone can request to create a device in a different
* zone.
*/
zid = crgetzoneid(credp);
if (zid != GLOBAL_ZONEID && via.via_zoneid != -1 &&
zid != via.via_zoneid) {
via.via_errno = VND_E_PERM;
ret = EIO;
goto errcopyout;
}
if (via.via_zoneid == -1)
via.via_zoneid = zid;
/*
* Establish the name we'll use now. We want to be extra paranoid about
* the device we're opening so check that now.
*/
if (zid == GLOBAL_ZONEID && via.via_zoneid != zid) {
zonep = zone_find_by_id(via.via_zoneid);
if (zonep == NULL) {
via.via_errno = VND_E_NOZONE;
ret = EIO;
goto errcopyout;
}
if (snprintf(NULL, 0, "/dev/net/zone/%s/%s", zonep->zone_name,
via.via_name) >= sizeof (buf)) {
zone_rele(zonep);
via.via_errno = VND_E_BADNAME;
ret = EIO;
goto errcopyout;
}
(void) snprintf(buf, sizeof (buf), "/dev/net/zone/%s/%s",
zonep->zone_name, via.via_name);
zone_rele(zonep);
zonep = NULL;
} else {
if (snprintf(NULL, 0, "/dev/net/%s", via.via_name) >=
sizeof (buf)) {
via.via_errno = VND_E_BADNAME;
ret = EIO;
goto errcopyout;
}
(void) snprintf(buf, sizeof (buf), "/dev/net/%s", via.via_name);
}
/*
* If our zone is dying then the netstack will have been removed from
* this list.
*/
nsp = vnd_nsd_lookup_by_zid(via.via_zoneid);
if (nsp == NULL) {
via.via_errno = VND_E_NOZONE;
ret = EIO;
goto errcopyout;
}
/*
* Note we set the attached handle even though we haven't actually
* finished the process of attaching the ldi handle.
*/
mutex_enter(&vdp->vdd_lock);
if (vdp->vdd_flags & (VND_D_ATTACHED | VND_D_ATTACH_INFLIGHT)) {
mutex_exit(&vdp->vdd_lock);
vnd_nsd_rele(nsp);
via.via_errno = VND_E_ATTACHED;
ret = EIO;
goto errcopyout;
}
vdp->vdd_flags |= VND_D_ATTACH_INFLIGHT;
ASSERT(vdp->vdd_cr == NULL);
crhold(credp);
vdp->vdd_cr = credp;
ASSERT(vdp->vdd_nsd == NULL);
vdp->vdd_nsd = nsp;
mutex_exit(&vdp->vdd_lock);
/*
* Place an additional hold on the vnd_pnsd_t as we go through and do
* all of the rest of our work. This will be the hold that we keep for
* as long as this thing is attached.
*/
vnd_nsd_ref(nsp);
ret = ldi_open_by_name(buf, FREAD | FWRITE, vdp->vdd_cr,
&vdp->vdd_ldih, vdp->vdd_ldiid);
if (ret != 0) {
if (ret == ENODEV)
via.via_errno = VND_E_NODATALINK;
goto err;
}
/*
* Unfortunately the I_PUSH interface doesn't allow us a way to detect
* whether or not we're coming in from a layered device. We really want
* to make sure that a normal user can't push on our streams module.
* Currently the only idea I have for this is to make sure that the
* credp is kcred which is really terrible.
*/
ret = ldi_ioctl(vdp->vdd_ldih, I_PUSH, (intptr_t)"vnd", FKIOCTL,
kcred, &rp);
if (ret != 0) {
rp = ldi_close(vdp->vdd_ldih, FREAD | FWRITE, vdp->vdd_cr);
VERIFY(rp == 0);
via.via_errno = VND_E_STRINIT;
ret = EIO;
goto err;
}
vss.vsa_minor = vdp->vdd_minor;
vss.vsa_nsid = nsp->vpnd_nsid;
ret = ldi_ioctl(vdp->vdd_ldih, VND_STRIOC_ASSOCIATE, (intptr_t)&vss,
FKIOCTL, kcred, &rp);
if (ret != 0 || vss.vsa_errno != VND_E_SUCCESS) {
rp = ldi_close(vdp->vdd_ldih, FREAD | FWRITE, vdp->vdd_cr);
VERIFY(rp == 0);
if (ret == 0) {
via.via_errno = vss.vsa_errno;
ret = EIO;
}
goto err;
}
mutex_enter(&vdp->vdd_nsd->vpnd_lock);
/*
* There's a chance that our netstack was condemned while we've had a
* hold on it. As such we need to check and if so, error out.
*/
if (vdp->vdd_nsd->vpnd_flags & VND_NS_CONDEMNED) {
mutex_exit(&vdp->vdd_nsd->vpnd_lock);
rp = ldi_close(vdp->vdd_ldih, FREAD | FWRITE, vdp->vdd_cr);
VERIFY(rp == 0);
ret = EIO;
via.via_errno = VND_E_NOZONE;
goto err;
}
mutex_enter(&vdp->vdd_lock);
VERIFY(vdp->vdd_str != NULL);
vdp->vdd_flags &= ~VND_D_ATTACH_INFLIGHT;
vdp->vdd_flags |= VND_D_ATTACHED;
(void) strlcpy(vdp->vdd_datalink, via.via_name,
sizeof (vdp->vdd_datalink));
list_insert_tail(&vdp->vdd_nsd->vpnd_dev_list, vdp);
mutex_exit(&vdp->vdd_lock);
mutex_exit(&vdp->vdd_nsd->vpnd_lock);
vnd_nsd_rele(nsp);
return (0);
err:
mutex_enter(&vdp->vdd_lock);
vdp->vdd_flags &= ~VND_D_ATTACH_INFLIGHT;
crfree(vdp->vdd_cr);
vdp->vdd_cr = NULL;
vdp->vdd_nsd = NULL;
mutex_exit(&vdp->vdd_lock);
/*
* We have two holds to drop here. One for our original reference and
* one for the hold this operation would have represented.
*/
vnd_nsd_rele(nsp);
vnd_nsd_rele(nsp);
errcopyout:
if (ddi_copyout(&via, (void *)arg, sizeof (via), cpflag) != 0)
ret = EFAULT;
return (ret);
}
static int
vnd_ioctl_link(vnd_dev_t *vdp, intptr_t arg, cred_t *credp, int cpflag)
{
int ret = 0;
vnd_ioc_link_t vil;
char mname[2*VND_NAMELEN];
char **c;
vnd_dev_t *v;
zoneid_t zid;
/* Not anyone can link something */
if (secpolicy_net_config(credp, B_FALSE) != 0)
return (EPERM);
if (ddi_copyin((void *)arg, &vil, sizeof (vil), cpflag) != 0)
return (EFAULT);
if (vnd_validate_name(vil.vil_name, VND_NAMELEN) == 0) {
ret = EIO;
vil.vil_errno = VND_E_BADNAME;
goto errcopyout;
}
c = vnd_reserved_names;
while (*c != NULL) {
if (strcmp(vil.vil_name, *c) == 0) {
ret = EIO;
vil.vil_errno = VND_E_BADNAME;
goto errcopyout;
}
c++;
}
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
ret = EIO;
vil.vil_errno = VND_E_NOTATTACHED;
goto errcopyout;
}
if (vdp->vdd_flags & VND_D_ZONE_DYING) {
mutex_exit(&vdp->vdd_lock);
ret = EIO;
vil.vil_errno = VND_E_NOZONE;
goto errcopyout;
}
if (vdp->vdd_flags & (VND_D_LINK_INFLIGHT | VND_D_LINKED)) {
mutex_exit(&vdp->vdd_lock);
ret = EIO;
vil.vil_errno = VND_E_LINKED;
goto errcopyout;
}
vdp->vdd_flags |= VND_D_LINK_INFLIGHT;
zid = vdp->vdd_nsd->vpnd_zid;
mutex_exit(&vdp->vdd_lock);
if (snprintf(NULL, 0, "z%d:%s", zid, vil.vil_name) >=
sizeof (mname)) {
ret = EIO;
vil.vil_errno = VND_E_BADNAME;
goto errcopyout;
}
mutex_enter(&vnd_dev_lock);
for (v = list_head(&vnd_dev_list); v != NULL;
v = list_next(&vnd_dev_list, v)) {
if (!(v->vdd_flags & VND_D_LINKED))
continue;
if (v->vdd_nsd->vpnd_zid == zid &&
strcmp(v->vdd_lname, vil.vil_name) == 0) {
mutex_exit(&vnd_dev_lock);
ret = EIO;
vil.vil_errno = VND_E_LINKEXISTS;
goto error;
}
}
/*
* We set the name and mark ourselves attached while holding the list
* lock to ensure that no other user can mistakingly find our name.
*/
(void) snprintf(mname, sizeof (mname), "z%d:%s", zid,
vil.vil_name);
mutex_enter(&vdp->vdd_lock);
/*
* Because we dropped our lock, we need to double check whether or not
* the zone was marked as dying while we were here. If it hasn't, then
* it's safe for us to link it in.
*/
if (vdp->vdd_flags & VND_D_ZONE_DYING) {
mutex_exit(&vdp->vdd_lock);
mutex_exit(&vnd_dev_lock);
ret = EIO;
vil.vil_errno = VND_E_NOZONE;
goto error;
}
(void) strlcpy(vdp->vdd_lname, vil.vil_name, sizeof (vdp->vdd_lname));
if (ddi_create_minor_node(vnd_dip, mname, S_IFCHR, vdp->vdd_minor,
DDI_PSEUDO, 0) != DDI_SUCCESS) {
ret = EIO;
vil.vil_errno = VND_E_MINORNODE;
} else {
vdp->vdd_flags &= ~VND_D_LINK_INFLIGHT;
vdp->vdd_flags |= VND_D_LINKED;
kstat_named_setstr(&vdp->vdd_str->vns_ksdata.vks_linkname,
vdp->vdd_lname);
ret = 0;
}
mutex_exit(&vdp->vdd_lock);
mutex_exit(&vnd_dev_lock);
if (ret == 0) {
/*
* Add a reference to represent that this device is linked into
* the file system name space to ensure that it doesn't
* disappear.
*/
vnd_dev_ref(vdp);
return (0);
}
error:
mutex_enter(&vdp->vdd_lock);
vdp->vdd_flags &= ~VND_D_LINK_INFLIGHT;
vdp->vdd_lname[0] = '\0';
mutex_exit(&vdp->vdd_lock);
errcopyout:
if (ddi_copyout(&vil, (void *)arg, sizeof (vil), cpflag) != 0)
ret = EFAULT;
return (ret);
}
/*
* Common unlink function. This is used both from the ioctl path and from the
* netstack shutdown path. The caller is required to hold the mutex on the
* vnd_dev_t, but they basically will have it relinquished for them. The only
* thing the caller is allowed to do afterward is to potentially rele the
* vnd_dev_t if they have their own hold. Note that only the ioctl path has its
* own hold.
*/
static void
vnd_dev_unlink(vnd_dev_t *vdp)
{
char mname[2*VND_NAMELEN];
ASSERT(MUTEX_HELD(&vdp->vdd_lock));
(void) snprintf(mname, sizeof (mname), "z%d:%s",
vdp->vdd_nsd->vpnd_zid, vdp->vdd_lname);
ddi_remove_minor_node(vnd_dip, mname);
vdp->vdd_lname[0] = '\0';
vdp->vdd_flags &= ~VND_D_LINKED;
kstat_named_setstr(&vdp->vdd_str->vns_ksdata.vks_linkname,
vdp->vdd_lname);
mutex_exit(&vdp->vdd_lock);
/*
* This rele corresponds to the reference that we took in
* vnd_ioctl_link.
*/
vnd_dev_rele(vdp);
}
static int
vnd_ioctl_unlink(vnd_dev_t *vdp, intptr_t arg, cred_t *credp, int cpflag)
{
int ret;
zoneid_t zid;
vnd_ioc_unlink_t viu;
/* Not anyone can unlink something */
if (secpolicy_net_config(credp, B_FALSE) != 0)
return (EPERM);
zid = crgetzoneid(credp);
if (ddi_copyin((void *)arg, &viu, sizeof (viu), cpflag) != 0)
return (EFAULT);
viu.viu_errno = VND_E_SUCCESS;
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_LINKED)) {
mutex_exit(&vdp->vdd_lock);
ret = EIO;
viu.viu_errno = VND_E_NOTLINKED;
goto err;
}
VERIFY(vdp->vdd_flags & VND_D_ATTACHED);
if (zid != GLOBAL_ZONEID && zid != vdp->vdd_nsd->vpnd_zid) {
mutex_exit(&vdp->vdd_lock);
ret = EIO;
viu.viu_errno = VND_E_PERM;
goto err;
}
/* vnd_dev_unlink releases the vdp mutex for us */
vnd_dev_unlink(vdp);
ret = 0;
err:
if (ddi_copyout(&viu, (void *)arg, sizeof (viu), cpflag) != 0)
return (EFAULT);
return (ret);
}
static int
vnd_ioctl_setrxbuf(vnd_dev_t *vdp, intptr_t arg, int cpflag)
{
int ret;
vnd_ioc_buf_t vib;
if (ddi_copyin((void *)arg, &vib, sizeof (vib), cpflag) != 0)
return (EFAULT);
mutex_enter(&vnd_dev_lock);
if (vib.vib_size > vnd_vdq_hard_max) {
mutex_exit(&vnd_dev_lock);
vib.vib_errno = VND_E_BUFTOOBIG;
ret = EIO;
goto err;
}
mutex_exit(&vnd_dev_lock);
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
vib.vib_errno = VND_E_NOTATTACHED;
ret = EIO;
goto err;
}
mutex_enter(&vdp->vdd_str->vns_lock);
if (vib.vib_size < vdp->vdd_str->vns_minwrite) {
mutex_exit(&vdp->vdd_str->vns_lock);
mutex_exit(&vdp->vdd_lock);
vib.vib_errno = VND_E_BUFTOOSMALL;
ret = EIO;
goto err;
}
mutex_exit(&vdp->vdd_str->vns_lock);
mutex_enter(&vdp->vdd_str->vns_dq_read.vdq_lock);
vdp->vdd_str->vns_dq_read.vdq_max = vib.vib_size;
mutex_exit(&vdp->vdd_str->vns_dq_read.vdq_lock);
mutex_exit(&vdp->vdd_lock);
ret = 0;
err:
if (ddi_copyout(&vib, (void *)arg, sizeof (vib), cpflag) != 0)
return (EFAULT);
return (ret);
}
static int
vnd_ioctl_getrxbuf(vnd_dev_t *vdp, intptr_t arg, int cpflag)
{
int ret;
vnd_ioc_buf_t vib;
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
vib.vib_errno = VND_E_NOTATTACHED;
ret = EIO;
goto err;
}
mutex_enter(&vdp->vdd_str->vns_dq_read.vdq_lock);
vib.vib_size = vdp->vdd_str->vns_dq_read.vdq_max;
mutex_exit(&vdp->vdd_str->vns_dq_read.vdq_lock);
mutex_exit(&vdp->vdd_lock);
ret = 0;
err:
if (ddi_copyout(&vib, (void *)arg, sizeof (vib), cpflag) != 0)
return (EFAULT);
return (ret);
}
static int
vnd_ioctl_getmaxbuf(vnd_dev_t *vdp, intptr_t arg, int cpflag)
{
vnd_ioc_buf_t vib;
mutex_enter(&vnd_dev_lock);
vib.vib_size = vnd_vdq_hard_max;
mutex_exit(&vnd_dev_lock);
if (ddi_copyout(&vib, (void *)arg, sizeof (vib), cpflag) != 0)
return (EFAULT);
return (0);
}
static int
vnd_ioctl_gettxbuf(vnd_dev_t *vdp, intptr_t arg, int cpflag)
{
int ret;
vnd_ioc_buf_t vib;
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
vib.vib_errno = VND_E_NOTATTACHED;
ret = EIO;
goto err;
}
mutex_enter(&vdp->vdd_str->vns_dq_write.vdq_lock);
vib.vib_size = vdp->vdd_str->vns_dq_write.vdq_max;
mutex_exit(&vdp->vdd_str->vns_dq_write.vdq_lock);
mutex_exit(&vdp->vdd_lock);
ret = 0;
err:
if (ddi_copyout(&vib, (void *)arg, sizeof (vib), cpflag) != 0)
return (EFAULT);
return (ret);
}
static int
vnd_ioctl_settxbuf(vnd_dev_t *vdp, intptr_t arg, int cpflag)
{
int ret;
vnd_ioc_buf_t vib;
if (ddi_copyin((void *)arg, &vib, sizeof (vib), cpflag) != 0)
return (EFAULT);
mutex_enter(&vnd_dev_lock);
if (vib.vib_size > vnd_vdq_hard_max) {
mutex_exit(&vnd_dev_lock);
vib.vib_errno = VND_E_BUFTOOBIG;
ret = EIO;
goto err;
}
mutex_exit(&vnd_dev_lock);
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
vib.vib_errno = VND_E_NOTATTACHED;
ret = EIO;
goto err;
}
mutex_enter(&vdp->vdd_str->vns_lock);
if (vib.vib_size < vdp->vdd_str->vns_minwrite) {
mutex_exit(&vdp->vdd_str->vns_lock);
mutex_exit(&vdp->vdd_lock);
vib.vib_errno = VND_E_BUFTOOSMALL;
ret = EIO;
goto err;
}
mutex_exit(&vdp->vdd_str->vns_lock);
mutex_enter(&vdp->vdd_str->vns_dq_write.vdq_lock);
vdp->vdd_str->vns_dq_write.vdq_max = vib.vib_size;
mutex_exit(&vdp->vdd_str->vns_dq_write.vdq_lock);
mutex_exit(&vdp->vdd_lock);
ret = 0;
err:
if (ddi_copyout(&vib, (void *)arg, sizeof (vib), cpflag) != 0)
return (EFAULT);
return (ret);
}
static int
vnd_ioctl_gettu(vnd_dev_t *vdp, intptr_t arg, int mode, boolean_t min)
{
vnd_ioc_buf_t vib;
vib.vib_errno = 0;
mutex_enter(&vdp->vdd_lock);
if (vdp->vdd_flags & VND_D_ATTACHED) {
mutex_enter(&vdp->vdd_str->vns_lock);
if (min == B_TRUE)
vib.vib_size = vdp->vdd_str->vns_minwrite;
else
vib.vib_size = vdp->vdd_str->vns_maxwrite;
mutex_exit(&vdp->vdd_str->vns_lock);
} else {
vib.vib_errno = VND_E_NOTATTACHED;
}
mutex_exit(&vdp->vdd_lock);
if (ddi_copyout(&vib, (void *)arg, sizeof (vib), mode & FKIOCTL) != 0)
return (EFAULT);
return (0);
}
static int
vnd_frameio_read(vnd_dev_t *vdp, intptr_t addr, int mode)
{
int ret, nonblock, nwrite;
frameio_t *fio;
vnd_data_queue_t *vqp;
mblk_t *mp;
fio = frameio_alloc(KM_NOSLEEP | KM_NORMALPRI);
if (fio == NULL)
return (EAGAIN);
ret = frameio_hdr_copyin(fio, FRAMEIO_NVECS_MAX, (const void *)addr,
mode);
if (ret != 0) {
frameio_free(fio);
return (ret);
}
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
frameio_free(fio);
return (ENXIO);
}
mutex_exit(&vdp->vdd_lock);
nonblock = mode & (FNONBLOCK | FNDELAY);
vqp = &vdp->vdd_str->vns_dq_read;
mutex_enter(&vqp->vdq_lock);
/* Check empty case */
if (vqp->vdq_cur == 0) {
if (nonblock != 0) {
mutex_exit(&vqp->vdq_lock);
frameio_free(fio);
return (EWOULDBLOCK);
}
while (vqp->vdq_cur == 0) {
if (cv_wait_sig(&vqp->vdq_ready, &vqp->vdq_lock) <= 0) {
mutex_exit(&vqp->vdq_lock);
frameio_free(fio);
return (EINTR);
}
}
}
ret = frameio_mblk_chain_write(fio, MAP_BLK_FRAME, vqp->vdq_head,
&nwrite, mode & FKIOCTL);
if (ret != 0) {
mutex_exit(&vqp->vdq_lock);
frameio_free(fio);
return (ret);
}
ret = frameio_hdr_copyout(fio, nwrite, (void *)addr, mode);
if (ret != 0) {
mutex_exit(&vqp->vdq_lock);
frameio_free(fio);
return (ret);
}
while (nwrite > 0) {
(void) vnd_dq_pop(vqp, &mp);
freemsg(mp);
nwrite--;
}
mutex_exit(&vqp->vdq_lock);
frameio_free(fio);
return (0);
}
static int
vnd_frameio_write(vnd_dev_t *vdp, intptr_t addr, int mode)
{
frameio_t *fio;
int ret, nonblock, nframes, i, nread;
size_t maxwrite, minwrite, total, flen;
mblk_t *mp_chain, *mp, *nmp;
vnd_data_queue_t *vqp;
fio = frameio_alloc(KM_NOSLEEP | KM_NORMALPRI);
if (fio == NULL)
return (EAGAIN);
ret = frameio_hdr_copyin(fio, FRAMEIO_NVECS_MAX, (void *)addr, mode);
if (ret != 0) {
frameio_free(fio);
return (ret);
}
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
frameio_free(fio);
return (ENXIO);
}
mutex_exit(&vdp->vdd_lock);
nonblock = mode & (FNONBLOCK | FNDELAY);
/*
* Make sure no single frame is larger than we can accept.
*/
mutex_enter(&vdp->vdd_str->vns_lock);
minwrite = vdp->vdd_str->vns_minwrite;
maxwrite = vdp->vdd_str->vns_maxwrite;
mutex_exit(&vdp->vdd_str->vns_lock);
nframes = fio->fio_nvpf / fio->fio_nvecs;
total = 0;
for (i = 0; i < nframes; i++) {
flen = frameio_frame_length(fio,
&fio->fio_vecs[i*fio->fio_nvpf]);
if (flen < minwrite || flen > maxwrite) {
frameio_free(fio);
return (ERANGE);
}
total += flen;
}
vqp = &vdp->vdd_str->vns_dq_write;
mutex_enter(&vqp->vdq_lock);
while (vnd_dq_reserve(vqp, total) == 0) {
if (nonblock != 0) {
frameio_free(fio);
mutex_exit(&vqp->vdq_lock);
return (EAGAIN);
}
if (cv_wait_sig(&vqp->vdq_ready, &vqp->vdq_lock) <= 0) {
mutex_exit(&vqp->vdq_lock);
frameio_free(fio);
return (EINTR);
}
}
mutex_exit(&vqp->vdq_lock);
/*
* We've reserved our space, let's copyin and go from here.
*/
ret = frameio_mblk_chain_read(fio, &mp_chain, &nread, mode & FKIOCTL);
if (ret != 0) {
frameio_free(fio);
vnd_dq_unreserve(vqp, total);
cv_broadcast(&vqp->vdq_ready);
pollwakeup(&vdp->vdd_ph, POLLOUT);
return (ret);
}
for (mp = mp_chain; mp != NULL; mp = nmp) {
nmp = mp->b_next;
mp->b_next = NULL;
gsqueue_enter_one(vdp->vdd_str->vns_squeue, mp,
vnd_squeue_tx_append, vdp->vdd_str, GSQUEUE_PROCESS,
VND_SQUEUE_TAG_VND_WRITE);
}
/*
* Update the frameio structure to indicate that we wrote those frames.
*/
frameio_mark_consumed(fio, nread);
ret = frameio_hdr_copyout(fio, nread, (void *)addr, mode);
frameio_free(fio);
return (ret);
}
static int
vnd_ioctl_list_copy_info(vnd_dev_t *vdp, vnd_ioc_info_t *arg, int mode)
{
const char *link;
uint32_t vers = 1;
ASSERT(MUTEX_HELD(&vdp->vdd_lock));
/*
* Copy all of the members out to userland.
*/
if (ddi_copyout(&vers, &arg->vii_version, sizeof (uint32_t),
mode & FKIOCTL) != 0)
return (EFAULT);
if (vdp->vdd_flags & VND_D_LINKED)
link = vdp->vdd_lname;
else
link = "<anonymous>";
if (ddi_copyout(link, arg->vii_name, sizeof (arg->vii_name),
mode & FKIOCTL) != 0)
return (EFAULT);
if (ddi_copyout(vdp->vdd_datalink, arg->vii_datalink,
sizeof (arg->vii_datalink), mode & FKIOCTL) != 0)
return (EFAULT);
if (ddi_copyout(&vdp->vdd_nsd->vpnd_zid, &arg->vii_zone,
sizeof (zoneid_t), mode & FKIOCTL) != 0)
return (EFAULT);
return (0);
}
static int
vnd_ioctl_list(intptr_t arg, cred_t *credp, int mode)
{
vnd_ioc_list_t vl;
vnd_ioc_list32_t vl32;
zoneid_t zid;
vnd_dev_t *vdp;
vnd_ioc_info_t *vip;
int found, cancopy, ret;
if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
if (ddi_copyin((void *)arg, &vl32, sizeof (vnd_ioc_list32_t),
mode & FKIOCTL) != 0)
return (EFAULT);
vl.vl_nents = vl32.vl_nents;
vl.vl_actents = vl32.vl_actents;
vl.vl_ents = (void *)(uintptr_t)vl32.vl_ents;
} else {
if (ddi_copyin((void *)arg, &vl, sizeof (vnd_ioc_list_t),
mode & FKIOCTL) != 0)
return (EFAULT);
}
cancopy = vl.vl_nents;
vip = vl.vl_ents;
found = 0;
zid = crgetzoneid(credp);
mutex_enter(&vnd_dev_lock);
for (vdp = list_head(&vnd_dev_list); vdp != NULL;
vdp = list_next(&vnd_dev_list, vdp)) {
mutex_enter(&vdp->vdd_lock);
if (vdp->vdd_flags & VND_D_ATTACHED &&
!(vdp->vdd_flags & (VND_D_CONDEMNED | VND_D_ZONE_DYING)) &&
(zid == GLOBAL_ZONEID || zid == vdp->vdd_nsd->vpnd_zid)) {
found++;
if (cancopy > 0) {
ret = vnd_ioctl_list_copy_info(vdp, vip, mode);
if (ret != 0) {
mutex_exit(&vdp->vdd_lock);
mutex_exit(&vnd_dev_lock);
return (ret);
}
cancopy--;
vip++;
}
}
mutex_exit(&vdp->vdd_lock);
}
mutex_exit(&vnd_dev_lock);
if (ddi_copyout(&found, &((vnd_ioc_list_t *)arg)->vl_actents,
sizeof (uint_t), mode & FKIOCTL) != 0)
return (EFAULT);
return (0);
}
static int
vnd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
int *rvalp)
{
int ret;
minor_t m;
vnd_dev_t *vdp;
m = getminor(dev);
ASSERT(m != 0);
/*
* Make sure no one has come in on an ioctl from the strioc case.
*/
if ((cmd & VND_STRIOC) == VND_STRIOC)
return (ENOTTY);
/*
* Like close, seems like if this minor isn't found, it's a programmer
* error somehow.
*/
vdp = vnd_dev_lookup(m);
if (vdp == NULL)
return (ENXIO);
switch (cmd) {
case VND_IOC_ATTACH:
if (!(mode & FWRITE)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_attach(vdp, arg, credp, mode);
break;
case VND_IOC_LINK:
if (!(mode & FWRITE)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_link(vdp, arg, credp, mode);
break;
case VND_IOC_UNLINK:
if (!(mode & FWRITE)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_unlink(vdp, arg, credp, mode);
break;
case VND_IOC_GETRXBUF:
if (!(mode & FREAD)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_getrxbuf(vdp, arg, mode);
break;
case VND_IOC_SETRXBUF:
if (!(mode & FWRITE)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_setrxbuf(vdp, arg, mode);
break;
case VND_IOC_GETTXBUF:
if (!(mode & FREAD)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_gettxbuf(vdp, arg, mode);
break;
case VND_IOC_SETTXBUF:
if (!(mode & FWRITE)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_settxbuf(vdp, arg, mode);
break;
case VND_IOC_GETMAXBUF:
if (!(mode & FREAD)) {
ret = EBADF;
break;
}
if (crgetzoneid(credp) != GLOBAL_ZONEID) {
ret = EPERM;
break;
}
ret = vnd_ioctl_getmaxbuf(vdp, arg, mode);
break;
case VND_IOC_GETMINTU:
if (!(mode & FREAD)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_gettu(vdp, arg, mode, B_TRUE);
break;
case VND_IOC_GETMAXTU:
if (!(mode & FREAD)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_gettu(vdp, arg, mode, B_FALSE);
break;
case VND_IOC_FRAMEIO_READ:
if (!(mode & FREAD)) {
ret = EBADF;
break;
}
ret = vnd_frameio_read(vdp, arg, mode);
break;
case VND_IOC_FRAMEIO_WRITE:
if (!(mode & FWRITE)) {
ret = EBADF;
break;
}
ret = vnd_frameio_write(vdp, arg, mode);
break;
case VND_IOC_LIST:
if (!(mode & FREAD)) {
ret = EBADF;
break;
}
ret = vnd_ioctl_list(arg, credp, mode);
break;
default:
ret = ENOTTY;
break;
}
vnd_dev_rele(vdp);
return (ret);
}
static int
vnd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
{
vnd_dev_t *vdp;
minor_t m;
zoneid_t zid;
if (flag & (FEXCL | FNDELAY))
return (ENOTSUP);
if (otyp & OTYP_BLK)
return (ENOTSUP);
zid = crgetzoneid(credp);
m = getminor(*devp);
/*
* If we have an open of a non-zero instance then we need to look that
* up in our list of entries.
*/
if (m != 0) {
/*
* We don't check for rawaccess globally as a user could be
* doing a list ioctl on the control node which doesn't require
* this privilege.
*/
if (secpolicy_net_rawaccess(credp) != 0)
return (EPERM);
vdp = vnd_dev_lookup(m);
if (vdp == NULL)
return (ENOENT);
/*
* We need to check to make sure that the user is allowed to
* open this node. At this point it should be an attached handle
* as that's all we're allowed to access.
*/
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_LINKED)) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (ENOENT);
}
if (vdp->vdd_flags & VND_D_ZONE_DYING) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (ENOENT);
}
if (zid != GLOBAL_ZONEID && zid != vdp->vdd_nsd->vpnd_zid) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (ENOENT);
}
if ((flag & FEXCL) && (vdp->vdd_flags & VND_D_OPENED)) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (EBUSY);
}
if (!(vdp->vdd_flags & VND_D_OPENED)) {
vdp->vdd_flags |= VND_D_OPENED;
vdp->vdd_ref++;
DTRACE_VND_REFINC(vdp);
}
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (0);
}
if (flag & FEXCL)
return (ENOTSUP);
/*
* We need to clone ourselves and set up new a state.
*/
vdp = kmem_cache_alloc(vnd_dev_cache, KM_SLEEP);
bzero(vdp, sizeof (vnd_dev_t));
if (ldi_ident_from_dev(*devp, &vdp->vdd_ldiid) != 0) {
kmem_cache_free(vnd_dev_cache, vdp);
return (EINVAL);
}
vdp->vdd_minor = id_alloc(vnd_minors);
mutex_init(&vdp->vdd_lock, NULL, MUTEX_DRIVER, NULL);
list_link_init(&vdp->vdd_link);
vdp->vdd_ref = 1;
*devp = makedevice(getmajor(*devp), vdp->vdd_minor);
vdp->vdd_devid = *devp;
DTRACE_VND_REFINC(vdp);
vdp->vdd_flags |= VND_D_OPENED;
mutex_enter(&vnd_dev_lock);
list_insert_head(&vnd_dev_list, vdp);
mutex_exit(&vnd_dev_lock);
return (0);
}
static int
vnd_close(dev_t dev, int flag, int otyp, cred_t *credp)
{
minor_t m;
vnd_dev_t *vdp;
m = getminor(dev);
if (m == 0)
return (ENXIO);
vdp = vnd_dev_lookup(m);
if (vdp == NULL)
return (ENXIO);
mutex_enter(&vdp->vdd_lock);
VERIFY(vdp->vdd_flags & VND_D_OPENED);
vdp->vdd_flags &= ~VND_D_OPENED;
mutex_exit(&vdp->vdd_lock);
/* Remove the hold from the previous open. */
vnd_dev_rele(vdp);
/* And now from lookup */
vnd_dev_rele(vdp);
return (0);
}
static int
vnd_read(dev_t dev, struct uio *uiop, cred_t *credp)
{
int nonblock, error = 0;
size_t mpsize;
vnd_dev_t *vdp;
vnd_data_queue_t *vqp;
mblk_t *mp = NULL;
offset_t u_loffset;
/*
* If we have more than one uio we refuse to do anything. That's for
* frameio.
*/
if (uiop->uio_iovcnt > 1)
return (EINVAL);
vdp = vnd_dev_lookup(getminor(dev));
if (vdp == NULL)
return (ENXIO);
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (ENXIO);
}
mutex_exit(&vdp->vdd_lock);
nonblock = uiop->uio_fmode & (FNONBLOCK | FNDELAY);
vqp = &vdp->vdd_str->vns_dq_read;
mutex_enter(&vqp->vdq_lock);
/* Check empty case */
if (vqp->vdq_cur == 0) {
if (nonblock != 0) {
error = EWOULDBLOCK;
goto err;
}
while (vqp->vdq_cur == 0) {
if (cv_wait_sig(&vqp->vdq_ready, &vqp->vdq_lock) <= 0) {
error = EINTR;
goto err;
}
}
}
/* Ensure our buffer is big enough */
mp = vqp->vdq_head;
ASSERT(mp != NULL);
mpsize = msgsize(mp);
if (mpsize > uiop->uio_resid) {
error = EOVERFLOW;
goto err;
}
u_loffset = uiop->uio_loffset;
while (mp != NULL) {
if (uiomove(mp->b_rptr, MBLKL(mp), UIO_READ, uiop) != 0) {
error = EFAULT;
uiop->uio_loffset = u_loffset;
mp = NULL;
goto err;
}
mpsize -= MBLKL(mp);
mp = mp->b_cont;
}
ASSERT(mpsize == 0);
(void) vnd_dq_pop(vqp, &mp);
freemsg(mp);
err:
mutex_exit(&vqp->vdq_lock);
vnd_dev_rele(vdp);
return (error);
}
static int
vnd_write(dev_t dev, struct uio *uiop, cred_t *credp)
{
int nonblock, error;
vnd_dev_t *vdp;
mblk_t *mp;
ssize_t iosize, origsize;
vnd_data_queue_t *vqp;
if (uiop->uio_iovcnt > 1)
return (EINVAL);
vdp = vnd_dev_lookup(getminor(dev));
if (vdp == NULL)
return (ENXIO);
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (ENXIO);
}
mutex_exit(&vdp->vdd_lock);
nonblock = uiop->uio_fmode & (FNONBLOCK | FNDELAY);
mutex_enter(&vdp->vdd_str->vns_lock);
if (uiop->uio_resid > vdp->vdd_str->vns_maxwrite ||
uiop->uio_resid < vdp->vdd_str->vns_minwrite) {
mutex_exit(&vdp->vdd_str->vns_lock);
vnd_dev_rele(vdp);
return (ERANGE);
}
mutex_exit(&vdp->vdd_str->vns_lock);
VERIFY(vdp->vdd_str != NULL);
/*
* Reserve space in the data queue if we can. If we can't, block or
* return EAGAIN. If we can, go and squeue_enter.
*/
vqp = &vdp->vdd_str->vns_dq_write;
mutex_enter(&vqp->vdq_lock);
while (vnd_dq_reserve(vqp, uiop->uio_resid) == 0) {
if (nonblock != 0) {
mutex_exit(&vqp->vdq_lock);
vnd_dev_rele(vdp);
return (EAGAIN);
}
if (cv_wait_sig(&vqp->vdq_ready, &vqp->vdq_lock) <= 0) {
mutex_exit(&vqp->vdq_lock);
vnd_dev_rele(vdp);
return (EINTR);
}
}
mutex_exit(&vqp->vdq_lock);
/*
* Now that we've reserved the space, try to allocate kernel space for
* and copy in the block. To take care of all this we use the
* strmakedata subroutine for now.
*/
origsize = iosize = uiop->uio_resid;
error = strmakedata(&iosize, uiop, vdp->vdd_str->vns_wq->q_stream, 0,
&mp);
/*
* strmakedata() will return an error or it may only consume a portion
* of the data.
*/
if (error != 0 || uiop->uio_resid != 0) {
vnd_dq_unreserve(vqp, origsize);
cv_broadcast(&vqp->vdq_ready);
pollwakeup(&vdp->vdd_ph, POLLOUT);
vnd_dev_rele(vdp);
return (ENOSR);
}
gsqueue_enter_one(vdp->vdd_str->vns_squeue, mp,
vnd_squeue_tx_append, vdp->vdd_str, GSQUEUE_PROCESS,
VND_SQUEUE_TAG_VND_WRITE);
vnd_dev_rele(vdp);
return (0);
}
static int
vnd_chpoll(dev_t dev, short events, int anyyet, short *reventsp,
struct pollhead **phpp)
{
int ready = 0;
vnd_dev_t *vdp;
vnd_data_queue_t *vqp;
vdp = vnd_dev_lookup(getminor(dev));
if (vdp == NULL)
return (ENXIO);
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_ATTACHED)) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (ENXIO);
}
mutex_exit(&vdp->vdd_lock);
if ((events & POLLIN) || (events & POLLRDNORM)) {
vqp = &vdp->vdd_str->vns_dq_read;
mutex_enter(&vqp->vdq_lock);
if (vqp->vdq_head != NULL)
ready |= events & (POLLIN | POLLRDNORM);
mutex_exit(&vqp->vdq_lock);
}
if (events & POLLOUT) {
vqp = &vdp->vdd_str->vns_dq_write;
mutex_enter(&vqp->vdq_lock);
if (vqp->vdq_cur != vqp->vdq_max)
ready |= POLLOUT;
mutex_exit(&vqp->vdq_lock);
}
if (ready != 0) {
*reventsp = ready;
vnd_dev_rele(vdp);
return (0);
}
*reventsp = 0;
if (!anyyet)
*phpp = &vdp->vdd_ph;
vnd_dev_rele(vdp);
return (0);
}
static void *
vnd_stack_init(netstackid_t stackid, netstack_t *ns)
{
vnd_pnsd_t *nsp;
nsp = kmem_cache_alloc(vnd_pnsd_cache, KM_SLEEP);
bzero(nsp, sizeof (*nsp));
nsp->vpnd_nsid = stackid;
nsp->vpnd_zid = netstackid_to_zoneid(stackid);
nsp->vpnd_flags = 0;
mutex_init(&nsp->vpnd_lock, NULL, MUTEX_DRIVER, NULL);
list_create(&nsp->vpnd_dev_list, sizeof (vnd_dev_t),
offsetof(vnd_dev_t, vdd_nslink));
if (vnd_netinfo_init(nsp) == 0)
nsp->vpnd_hooked = B_TRUE;
mutex_enter(&vnd_dev_lock);
list_insert_tail(&vnd_nsd_list, nsp);
mutex_exit(&vnd_dev_lock);
return (nsp);
}
static void
vnd_stack_shutdown(netstackid_t stackid, void *arg)
{
vnd_pnsd_t *nsp = arg;
vnd_dev_t *vdp;
ASSERT(nsp != NULL);
/*
* After shut down no one should be able to find their way to this
* netstack again.
*/
mutex_enter(&vnd_dev_lock);
list_remove(&vnd_nsd_list, nsp);
mutex_exit(&vnd_dev_lock);
/*
* Make sure hooks know that they're going away.
*/
if (nsp->vpnd_hooked == B_TRUE)
vnd_netinfo_shutdown(nsp);
/*
* Now we need to go through and notify each zone that they are in
* teardown phase. See the big theory statement section on vnd, zones,
* netstacks, and sdev for more information about this.
*/
mutex_enter(&nsp->vpnd_lock);
nsp->vpnd_flags |= VND_NS_CONDEMNED;
for (vdp = list_head(&nsp->vpnd_dev_list); vdp != NULL;
vdp = list_next(&nsp->vpnd_dev_list, vdp)) {
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_CONDEMNED))
vdp->vdd_flags |= VND_D_ZONE_DYING;
mutex_exit(&vdp->vdd_lock);
}
mutex_exit(&nsp->vpnd_lock);
/*
* Next we remove all the links as we know nothing new can be added to
* the list and that none of the extent devices can obtain additional
* links.
*/
restart:
mutex_enter(&nsp->vpnd_lock);
for (vdp = list_head(&nsp->vpnd_dev_list); vdp != NULL;
vdp = list_next(&nsp->vpnd_dev_list, vdp)) {
mutex_enter(&vdp->vdd_lock);
if ((vdp->vdd_flags & VND_D_CONDEMNED) ||
!(vdp->vdd_flags & VND_D_LINKED)) {
mutex_exit(&vdp->vdd_lock);
continue;
}
/*
* We drop our lock here and restart afterwards. Note that as
* part of unlinking we end up doing a rele of the vnd_dev_t. If
* this is the final hold on the vnd_dev_t then it might try and
* remove itself. Our locking rules requires not to be holding
* any locks when we call any of the rele functions.
*
* Note that the unlink function requires holders to call into
* it with the vnd_dev_t->vdd_lock held and will take care of it
* for us. Because we don't have a hold on it, we're done at
* this point.
*/
mutex_exit(&nsp->vpnd_lock);
/* Forcibly unlink */
vnd_dev_unlink(vdp);
goto restart;
}
mutex_exit(&nsp->vpnd_lock);
}
static void
vnd_stack_destroy(netstackid_t stackid, void *arg)
{
vnd_pnsd_t *nsp = arg;
ASSERT(nsp != NULL);
/*
* Now that we've unlinked everything we just have to hang out for
* it to finish exiting. Now that it's no longer the kernel itself
* that's doing this we just need to wait for our reference count to
* equal zero and then we're free. If the global zone is holding open a
* reference to a vnd device for another zone, that's bad, but there's
* nothing much we can do. See the section on 'vnd, zones, netstacks' in
* the big theory statement for more information.
*/
mutex_enter(&nsp->vpnd_lock);
while (nsp->vpnd_ref != 0)
cv_wait(&nsp->vpnd_ref_change, &nsp->vpnd_lock);
mutex_exit(&nsp->vpnd_lock);
/*
* During shutdown we removed ourselves from the list and now we have no
* more references so we can safely say that there is nothing left and
* destroy everything that we had sitting around.
*/
if (nsp->vpnd_hooked == B_TRUE)
vnd_netinfo_fini(nsp);
mutex_destroy(&nsp->vpnd_lock);
list_destroy(&nsp->vpnd_dev_list);
kmem_cache_free(vnd_pnsd_cache, nsp);
}
/*
* Convert a node with a name of the form /dev/vnd/zone/%zonename and
* /dev/vnd/zone/%zonename/%linkname to the corresponding vnd netstack.
*/
static vnd_pnsd_t *
vnd_sdev_ctx_to_ns(sdev_ctx_t ctx)
{
enum vtype vt;
const char *path = sdev_ctx_path(ctx);
char *zstart, *dup;
size_t duplen;
vnd_pnsd_t *nsp;
vt = sdev_ctx_vtype(ctx);
ASSERT(strncmp(path, VND_SDEV_ZROOT, strlen(VND_SDEV_ZROOT)) == 0);
if (vt == VDIR) {
zstart = strrchr(path, '/');
ASSERT(zstart != NULL);
zstart++;
return (vnd_nsd_lookup_by_zonename(zstart));
}
ASSERT(vt == VCHR);
dup = strdup(path);
duplen = strlen(dup) + 1;
zstart = strrchr(dup, '/');
*zstart = '\0';
zstart--;
zstart = strrchr(dup, '/');
zstart++;
nsp = vnd_nsd_lookup_by_zonename(zstart);
kmem_free(dup, duplen);
return (nsp);
}
static sdev_plugin_validate_t
vnd_sdev_validate_dir(sdev_ctx_t ctx)
{
vnd_pnsd_t *nsp;
if (strcmp(sdev_ctx_path(ctx), VND_SDEV_ROOT) == 0)
return (SDEV_VTOR_VALID);
if (strcmp(sdev_ctx_path(ctx), VND_SDEV_ZROOT) == 0) {
ASSERT(getzoneid() == GLOBAL_ZONEID);
ASSERT(sdev_ctx_flags(ctx) & SDEV_CTX_GLOBAL);
return (SDEV_VTOR_VALID);
}
nsp = vnd_sdev_ctx_to_ns(ctx);
if (nsp == NULL)
return (SDEV_VTOR_INVALID);
vnd_nsd_rele(nsp);
return (SDEV_VTOR_VALID);
}
static sdev_plugin_validate_t
vnd_sdev_validate(sdev_ctx_t ctx)
{
enum vtype vt;
dev_t dev;
vnd_dev_t *vdp;
vt = sdev_ctx_vtype(ctx);
if (vt == VDIR)
return (vnd_sdev_validate_dir(ctx));
ASSERT(vt == VCHR);
if (strcmp("ctl", sdev_ctx_name(ctx)) == 0)
return (SDEV_VTOR_VALID);
dev = (uintptr_t)sdev_ctx_vtype_data(ctx);
vdp = vnd_dev_lookup(getminor(dev));
if (vdp == NULL)
return (SDEV_VTOR_STALE);
mutex_enter(&vdp->vdd_lock);
if (!(vdp->vdd_flags & VND_D_LINKED) ||
(vdp->vdd_flags & (VND_D_CONDEMNED | VND_D_ZONE_DYING))) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (SDEV_VTOR_STALE);
}
if (strcmp(sdev_ctx_name(ctx), vdp->vdd_lname) != 0) {
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (SDEV_VTOR_STALE);
}
mutex_exit(&vdp->vdd_lock);
vnd_dev_rele(vdp);
return (SDEV_VTOR_VALID);
}
/*
* This function is a no-op. sdev never has holds on our devices as they can go
* away at any time and specfs has to deal with that fact.
*/
static void
vnd_sdev_inactive(sdev_ctx_t ctx)
{
}
static int
vnd_sdev_fillzone(vnd_pnsd_t *nsp, sdev_ctx_t ctx)
{
int ret;
vnd_dev_t *vdp;
mutex_enter(&nsp->vpnd_lock);
for (vdp = list_head(&nsp->vpnd_dev_list); vdp != NULL;
vdp = list_next(&nsp->vpnd_dev_list, vdp)) {
mutex_enter(&vdp->vdd_lock);
if ((vdp->vdd_flags & VND_D_LINKED) &&
!(vdp->vdd_flags & (VND_D_CONDEMNED | VND_D_ZONE_DYING))) {
ret = sdev_plugin_mknod(ctx, vdp->vdd_lname, S_IFCHR,
vdp->vdd_devid);
if (ret != 0 && ret != EEXIST) {
mutex_exit(&vdp->vdd_lock);
mutex_exit(&nsp->vpnd_lock);
vnd_nsd_rele(nsp);
return (ret);
}
}
mutex_exit(&vdp->vdd_lock);
}
mutex_exit(&nsp->vpnd_lock);
return (0);
}
static int
vnd_sdev_filldir_root(sdev_ctx_t ctx)
{
zoneid_t zid;
vnd_pnsd_t *nsp;
int ret;
zid = getzoneid();
nsp = vnd_nsd_lookup(zoneid_to_netstackid(zid));
ASSERT(nsp != NULL);
ret = vnd_sdev_fillzone(nsp, ctx);
vnd_nsd_rele(nsp);
if (ret != 0)
return (ret);
/*
* Checking the zone id is not sufficient as the global zone could be
* reaching down into a non-global zone's mounted /dev.
*/
if (zid == GLOBAL_ZONEID && (sdev_ctx_flags(ctx) & SDEV_CTX_GLOBAL)) {
ret = sdev_plugin_mkdir(ctx, "zone");
if (ret != 0 && ret != EEXIST)
return (ret);
}
/*
* Always add a reference to the control node. There's no need to
* reference it since it always exists and is always what we clone from.
*/
ret = sdev_plugin_mknod(ctx, "ctl", S_IFCHR,
makedevice(ddi_driver_major(vnd_dip), 0));
if (ret != 0 && ret != EEXIST)
return (ret);
return (0);
}
static int
vnd_sdev_filldir_zroot(sdev_ctx_t ctx)
{
int ret;
vnd_pnsd_t *nsp;
zone_t *zonep;
ASSERT(getzoneid() == GLOBAL_ZONEID);
ASSERT(sdev_ctx_flags(ctx) & SDEV_CTX_GLOBAL);
mutex_enter(&vnd_dev_lock);
for (nsp = list_head(&vnd_nsd_list); nsp != NULL;
nsp = list_next(&vnd_nsd_list, nsp)) {
mutex_enter(&nsp->vpnd_lock);
if (list_is_empty(&nsp->vpnd_dev_list)) {
mutex_exit(&nsp->vpnd_lock);
continue;
}
mutex_exit(&nsp->vpnd_lock);
zonep = zone_find_by_id(nsp->vpnd_zid);
/*
* This zone must be being torn down, so skip it.
*/
if (zonep == NULL)
continue;
ret = sdev_plugin_mkdir(ctx, zonep->zone_name);
zone_rele(zonep);
if (ret != 0 && ret != EEXIST) {
mutex_exit(&vnd_dev_lock);
return (ret);
}
}
mutex_exit(&vnd_dev_lock);
return (0);
}
static int
vnd_sdev_filldir(sdev_ctx_t ctx)
{
int ret;
vnd_pnsd_t *nsp;
ASSERT(sdev_ctx_vtype(ctx) == VDIR);
if (strcmp(VND_SDEV_ROOT, sdev_ctx_path(ctx)) == 0)
return (vnd_sdev_filldir_root(ctx));
if (strcmp(VND_SDEV_ZROOT, sdev_ctx_path(ctx)) == 0)
return (vnd_sdev_filldir_zroot(ctx));
ASSERT(strncmp(VND_SDEV_ZROOT, sdev_ctx_path(ctx),
strlen(VND_SDEV_ZROOT)) == 0);
nsp = vnd_sdev_ctx_to_ns(ctx);
if (nsp == NULL)
return (0);
ret = vnd_sdev_fillzone(nsp, ctx);
vnd_nsd_rele(nsp);
return (ret);
}
static sdev_plugin_ops_t vnd_sdev_ops = {
SDEV_PLUGIN_VERSION,
SDEV_PLUGIN_SUBDIR,
vnd_sdev_validate,
vnd_sdev_filldir,
vnd_sdev_inactive
};
static int
vnd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int errp = 0;
if (cmd != DDI_ATTACH)
return (DDI_FAILURE);
/*
* Only allow one instance.
*/
if (vnd_dip != NULL)
return (DDI_FAILURE);
vnd_dip = dip;
if (ddi_create_minor_node(vnd_dip, "vnd", S_IFCHR, 0, DDI_PSEUDO, 0) !=
DDI_SUCCESS) {
vnd_dip = NULL;
return (DDI_FAILURE);
}
if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
ddi_remove_minor_node(vnd_dip, NULL);
vnd_dip = NULL;
return (DDI_FAILURE);
}
vnd_sdev_hdl = sdev_plugin_register(VND_SDEV_NAME, &vnd_sdev_ops,
&errp);
if (vnd_sdev_hdl == NULL) {
ddi_remove_minor_node(vnd_dip, NULL);
ddi_prop_remove_all(vnd_dip);
vnd_dip = NULL;
return (DDI_FAILURE);
}
vnd_sqset = gsqueue_set_create(GSQUEUE_DEFAULT_WAIT,
GSQUEUE_DEFAULT_PRIORITY);
return (DDI_SUCCESS);
}
static int
vnd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
if (cmd != DDI_DETACH)
return (DDI_FAILURE);
mutex_enter(&vnd_dev_lock);
if (!list_is_empty(&vnd_dev_list)) {
mutex_exit(&vnd_dev_lock);
return (DDI_FAILURE);
}
mutex_exit(&vnd_dev_lock);
return (DDI_FAILURE);
}
static int
vnd_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
{
int error;
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
*result = (void *)vnd_dip;
error = DDI_SUCCESS;
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)0;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
break;
}
return (error);
}
static void
vnd_ddi_fini(void)
{
netstack_unregister(NS_VND);
if (vnd_taskq != NULL)
taskq_destroy(vnd_taskq);
if (vnd_str_cache != NULL)
kmem_cache_destroy(vnd_str_cache);
if (vnd_dev_cache != NULL)
kmem_cache_destroy(vnd_dev_cache);
if (vnd_pnsd_cache != NULL)
kmem_cache_destroy(vnd_pnsd_cache);
if (vnd_minors != NULL)
id_space_destroy(vnd_minors);
if (vnd_list_init != 0) {
list_destroy(&vnd_nsd_list);
list_destroy(&vnd_dev_list);
mutex_destroy(&vnd_dev_lock);
vnd_list_init = 0;
}
frameio_fini();
}
static int
vnd_ddi_init(void)
{
if (frameio_init() != 0)
return (DDI_FAILURE);
vnd_str_cache = kmem_cache_create("vnd_str_cache", sizeof (vnd_str_t),
0, NULL, NULL, NULL, NULL, NULL, 0);
if (vnd_str_cache == NULL) {
frameio_fini();
return (DDI_FAILURE);
}
vnd_dev_cache = kmem_cache_create("vnd_dev_cache", sizeof (vnd_dev_t),
0, NULL, NULL, NULL, NULL, NULL, 0);
if (vnd_dev_cache == NULL) {
kmem_cache_destroy(vnd_str_cache);
frameio_fini();
return (DDI_FAILURE);
}
vnd_pnsd_cache = kmem_cache_create("vnd_pnsd_cache",
sizeof (vnd_pnsd_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
if (vnd_pnsd_cache == NULL) {
kmem_cache_destroy(vnd_dev_cache);
kmem_cache_destroy(vnd_str_cache);
frameio_fini();
return (DDI_FAILURE);
}
vnd_taskq = taskq_create_instance("vnd", -1, 1, minclsyspri, 0, 0, 0);
if (vnd_taskq == NULL) {
kmem_cache_destroy(vnd_pnsd_cache);
kmem_cache_destroy(vnd_dev_cache);
kmem_cache_destroy(vnd_str_cache);
frameio_fini();
return (DDI_FAILURE);
}
vnd_minors = id_space_create("vnd_minors", 1, INT32_MAX);
if (vnd_minors == NULL) {
taskq_destroy(vnd_taskq);
kmem_cache_destroy(vnd_pnsd_cache);
kmem_cache_destroy(vnd_dev_cache);
kmem_cache_destroy(vnd_str_cache);
frameio_fini();
return (DDI_FAILURE);
}
mutex_init(&vnd_dev_lock, NULL, MUTEX_DRIVER, NULL);
list_create(&vnd_dev_list, sizeof (vnd_dev_t),
offsetof(vnd_dev_t, vdd_link));
list_create(&vnd_nsd_list, sizeof (vnd_pnsd_t),
offsetof(vnd_pnsd_t, vpnd_link));
vnd_list_init = 1;
netstack_register(NS_VND, vnd_stack_init, vnd_stack_shutdown,
vnd_stack_destroy);
return (DDI_SUCCESS);
}
static struct module_info vnd_minfo = {
0, /* module id */
"vnd", /* module name */
1, /* smallest packet size */
INFPSZ, /* largest packet size (infinite) */
1, /* high watermark */
0 /* low watermark */
};
static struct qinit vnd_r_qinit = {
vnd_s_rput,
NULL,
vnd_s_open,
vnd_s_close,
NULL,
&vnd_minfo,
NULL
};
static struct qinit vnd_w_qinit = {
vnd_s_wput,
NULL,
NULL,
NULL,
NULL,
&vnd_minfo,
NULL
};
static struct streamtab vnd_strtab = {
&vnd_r_qinit,
&vnd_w_qinit,
NULL,
NULL
};
static struct cb_ops vnd_cb_ops = {
vnd_open, /* open */
vnd_close, /* close */
nulldev, /* strategy */
nulldev, /* print */
nodev, /* dump */
vnd_read, /* read */
vnd_write, /* write */
vnd_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
vnd_chpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
NULL, /* streamtab */
D_MP /* Driver compatibility flag */
};
static struct dev_ops vnd_dev_ops = {
DEVO_REV, /* devo_rev */
0, /* refcnt */
vnd_info, /* get_dev_info */
nulldev, /* identify */
nulldev, /* probe */
vnd_attach, /* attach */
vnd_detach, /* detach */
nodev, /* reset */
&vnd_cb_ops, /* driver operations */
NULL, /* bus operations */
nodev, /* dev power */
ddi_quiesce_not_needed /* quiesce */
};
static struct modldrv vnd_modldrv = {
&mod_driverops,
"Virtual Networking Datapath Driver",
&vnd_dev_ops
};
static struct fmodsw vnd_fmodfsw = {
"vnd",
&vnd_strtab,
D_NEW | D_MP
};
static struct modlstrmod vnd_modlstrmod = {
&mod_strmodops,
"Virtual Networking Datapath Driver",
&vnd_fmodfsw
};
static struct modlinkage vnd_modlinkage = {
MODREV_1,
&vnd_modldrv,
&vnd_modlstrmod,
NULL
};
int
_init(void)
{
int error;
/*
* We need to do all of our global initialization in init as opposed to
* attach and detach. The problem here is that because vnd can be used
* from a stream context while being detached, we can not rely on having
* run attach to create everything, alas. so it goes in _init, just like
* our friend ip.
*/
if ((error = vnd_ddi_init()) != DDI_SUCCESS)
return (error);
error = mod_install((&vnd_modlinkage));
if (error != 0)
vnd_ddi_fini();
return (error);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&vnd_modlinkage, modinfop));
}
int
_fini(void)
{
int error;
error = mod_remove(&vnd_modlinkage);
if (error == 0)
vnd_ddi_fini();
return (error);
}
|