Loading...
   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
/*
 * Copyright (c) 2009-2024 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/filedesc.h>
#include <sys/file_internal.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/errno.h>
#include <sys/protosw.h>
#include <sys/domain.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <sys/sysproto.h>

#include <net/if.h>
#include <net/if_var.h>
#include <net/route.h>

#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/in_pcb.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet/tcp.h>
#include <netinet/tcp_var.h>
#include <netinet/tcp_cc.h>
#include <netinet/in_tclass.h>

#include <os/log.h>

static_assert(_SO_TC_MAX == SO_TC_STATS_MAX);

/*
 * The size is one more than the max because DSCP start at zero
 */
#define DSCP_ARRAY_SIZE (_MAX_DSCP + 1)

struct net_qos_dscp_map {
	uint8_t        sotc_to_dscp[SO_TC_MAX];
	uint8_t        netsvctype_to_dscp[_NET_SERVICE_TYPE_COUNT];
};

struct dcsp_msc_map {
	uint8_t                 dscp;
	mbuf_svc_class_t        msc;
};
static inline int so_throttle_best_effort(struct socket *, struct ifnet *);
static void set_dscp_to_wifi_ac_map(const struct dcsp_msc_map *__indexable, int);
static errno_t dscp_msc_map_from_netsvctype_dscp_map(struct netsvctype_dscp_map *__counted_by(count) map,
    size_t count, struct dcsp_msc_map *__counted_by(DSCP_ARRAY_SIZE));

SYSCTL_NODE(_net, OID_AUTO, qos,
    CTLFLAG_RW | CTLFLAG_LOCKED, 0, "QoS");

static int sysctl_default_netsvctype_to_dscp_map SYSCTL_HANDLER_ARGS;
SYSCTL_PROC(_net_qos, OID_AUTO, default_netsvctype_to_dscp_map,
    CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED,
    0, 0, sysctl_default_netsvctype_to_dscp_map, "S", "");

static int sysctl_dscp_to_wifi_ac_map SYSCTL_HANDLER_ARGS;
SYSCTL_PROC(_net_qos, OID_AUTO, dscp_to_wifi_ac_map,
    CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED,
    0, 0, sysctl_dscp_to_wifi_ac_map, "S", "");

static int sysctl_reset_dscp_to_wifi_ac_map SYSCTL_HANDLER_ARGS;
SYSCTL_PROC(_net_qos, OID_AUTO, reset_dscp_to_wifi_ac_map,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, 0, sysctl_reset_dscp_to_wifi_ac_map, "I", "");

int net_qos_verbose = 0;
SYSCTL_INT(_net_qos, OID_AUTO, verbose,
    CTLFLAG_RW | CTLFLAG_LOCKED, &net_qos_verbose, 0, "");

/*
 * Fastlane QoS policy:
 * By Default allow all apps to get traffic class to DSCP mapping
 */
SYSCTL_NODE(_net_qos, OID_AUTO, policy,
    CTLFLAG_RW | CTLFLAG_LOCKED, 0, "");

int net_qos_policy_restricted = 0;
SYSCTL_INT(_net_qos_policy, OID_AUTO, restricted,
    CTLFLAG_RW | CTLFLAG_LOCKED, &net_qos_policy_restricted, 0, "");

int net_qos_policy_restrict_avapps = 0;
SYSCTL_INT(_net_qos_policy, OID_AUTO, restrict_avapps,
    CTLFLAG_RW | CTLFLAG_LOCKED, &net_qos_policy_restrict_avapps, 0, "");

int net_qos_policy_wifi_enabled = 0;
SYSCTL_INT(_net_qos_policy, OID_AUTO, wifi_enabled,
    CTLFLAG_RW | CTLFLAG_LOCKED, &net_qos_policy_wifi_enabled, 0, "");

int net_qos_policy_capable_enabled = 0;
SYSCTL_INT(_net_qos_policy, OID_AUTO, capable_enabled,
    CTLFLAG_RW | CTLFLAG_LOCKED, &net_qos_policy_capable_enabled, 0, "");

/*
 * Socket traffic class from network service type
 */
const int sotc_by_netservicetype[_NET_SERVICE_TYPE_COUNT] = {
	SO_TC_BE,       /* NET_SERVICE_TYPE_BE */
	SO_TC_BK,       /* NET_SERVICE_TYPE_BK */
	SO_TC_VI,       /* NET_SERVICE_TYPE_SIG */
	SO_TC_VI,       /* NET_SERVICE_TYPE_VI */
	SO_TC_VO,       /* NET_SERVICE_TYPE_VO */
	SO_TC_RV,       /* NET_SERVICE_TYPE_RV */
	SO_TC_AV,       /* NET_SERVICE_TYPE_AV */
	SO_TC_OAM,      /* NET_SERVICE_TYPE_OAM */
	SO_TC_RD        /* NET_SERVICE_TYPE_RD */
};

/*
 * DSCP mappings for QoS Fastlane as based on network service types
 */
static const
struct netsvctype_dscp_map fastlane_netsvctype_dscp_map[_NET_SERVICE_TYPE_COUNT] = {
	{ .netsvctype = NET_SERVICE_TYPE_BE, .dscp = _DSCP_DF },
	{ .netsvctype = NET_SERVICE_TYPE_BK, .dscp = _DSCP_AF11 },
	{ .netsvctype = NET_SERVICE_TYPE_SIG, .dscp = _DSCP_CS3 },
	{ .netsvctype = NET_SERVICE_TYPE_VI, .dscp = _DSCP_AF41 },
	{ .netsvctype = NET_SERVICE_TYPE_VO, .dscp = _DSCP_EF },
	{ .netsvctype = NET_SERVICE_TYPE_RV, .dscp = _DSCP_CS4 },
	{ .netsvctype = NET_SERVICE_TYPE_AV, .dscp = _DSCP_AF31 },
	{ .netsvctype = NET_SERVICE_TYPE_OAM, .dscp = _DSCP_CS2 },
	{ .netsvctype = NET_SERVICE_TYPE_RD, .dscp = _DSCP_AF21 },
};

/*
 * DSCP mappings for QoS RFC4594 as based on network service types
 */
static const
struct netsvctype_dscp_map rfc4594_netsvctype_dscp_map[_NET_SERVICE_TYPE_COUNT] = {
	{ .netsvctype = NET_SERVICE_TYPE_BE, .dscp = _DSCP_DF },
	{ .netsvctype = NET_SERVICE_TYPE_BK, .dscp = _DSCP_CS1 },
	{ .netsvctype = NET_SERVICE_TYPE_SIG, .dscp = _DSCP_CS5 },
	{ .netsvctype = NET_SERVICE_TYPE_VI, .dscp = _DSCP_AF41 },
	{ .netsvctype = NET_SERVICE_TYPE_VO, .dscp = _DSCP_EF },
	{ .netsvctype = NET_SERVICE_TYPE_RV, .dscp = _DSCP_CS4 },
	{ .netsvctype = NET_SERVICE_TYPE_AV, .dscp = _DSCP_AF31 },
	{ .netsvctype = NET_SERVICE_TYPE_OAM, .dscp = _DSCP_CS2 },
	{ .netsvctype = NET_SERVICE_TYPE_RD, .dscp = _DSCP_AF21 },
};

static struct net_qos_dscp_map fastlane_net_qos_dscp_map;
static struct net_qos_dscp_map rfc4594_net_qos_dscp_map;
#if (DEBUG || DEVELOPMENT)
static struct net_qos_dscp_map custom_net_qos_dscp_map;
#endif /* (DEBUG || DEVELOPMENT) */


/*
 * The DSCP to UP mapping (via mbuf service class) for WiFi follows is the mapping
 * that implemented at the 802.11 driver level when the mbuf service class is
 * MBUF_SC_BE.
 *
 * This clashes with the recommended mapping documented by the IETF document
 * draft-szigeti-tsvwg-ieee-802-11e-01.txt but we keep the mapping to maintain
 * binary compatibility. Applications should use the network service type socket
 * option instead to select L2 QoS marking instead of IP_TOS or IPV6_TCLASS.
 */
static const struct dcsp_msc_map default_dscp_to_wifi_ac_map[] = {
	{ .dscp = _DSCP_DF, .msc = MBUF_SC_BE },        /* RFC 2474 Standard */
	{ .dscp = 1, .msc = MBUF_SC_BE },               /*  */
	{ .dscp = 2, .msc = MBUF_SC_BE },               /*  */
	{ .dscp = 3, .msc = MBUF_SC_BE },               /*  */
	{ .dscp = 4, .msc = MBUF_SC_BE },               /*  */
	{ .dscp = 5, .msc = MBUF_SC_BE },               /*  */
	{ .dscp = 6, .msc = MBUF_SC_BE },               /*  */
	{ .dscp = 7, .msc = MBUF_SC_BE },               /*  */

	{ .dscp = _DSCP_CS1, .msc = MBUF_SC_BK },       /* RFC 3662 Low-Priority Data */
	{ .dscp = 9, .msc = MBUF_SC_BK },               /*  */
	{ .dscp = _DSCP_AF11, .msc = MBUF_SC_BK },      /* RFC 2597 High-Throughput Data */
	{ .dscp = 11, .msc = MBUF_SC_BK },              /*  */
	{ .dscp = _DSCP_AF12, .msc = MBUF_SC_BK },      /* RFC 2597 High-Throughput Data */
	{ .dscp = 13, .msc = MBUF_SC_BK },              /*  */
	{ .dscp = _DSCP_AF13, .msc = MBUF_SC_BK },      /* RFC 2597 High-Throughput Data */
	{ .dscp = 15, .msc = MBUF_SC_BK },              /*  */

	{ .dscp = _DSCP_CS2, .msc = MBUF_SC_BK },       /* RFC 4594 OAM */
	{ .dscp = 17, .msc = MBUF_SC_BK },              /*  */
	{ .dscp = _DSCP_AF21, .msc = MBUF_SC_BK },      /* RFC 2597 Low-Latency Data */
	{ .dscp = 19, .msc = MBUF_SC_BK },              /*  */
	{ .dscp = _DSCP_AF22, .msc = MBUF_SC_BK },      /* RFC 2597 Low-Latency Data */
	{ .dscp = 21, .msc = MBUF_SC_BK },              /*  */
	{ .dscp = _DSCP_AF23, .msc = MBUF_SC_BK },      /* RFC 2597 Low-Latency Data */
	{ .dscp = 23, .msc = MBUF_SC_BK },              /*  */

	{ .dscp = _DSCP_CS3, .msc = MBUF_SC_BE },       /* RFC 2474 Broadcast Video */
	{ .dscp = 25, .msc = MBUF_SC_BE },              /*  */
	{ .dscp = _DSCP_AF31, .msc = MBUF_SC_BE },      /* RFC 2597 Multimedia Streaming */
	{ .dscp = 27, .msc = MBUF_SC_BE },              /*  */
	{ .dscp = _DSCP_AF32, .msc = MBUF_SC_BE },      /* RFC 2597 Multimedia Streaming */
	{ .dscp = 29, .msc = MBUF_SC_BE },              /*  */
	{ .dscp = _DSCP_AF33, .msc = MBUF_SC_BE },      /* RFC 2597 Multimedia Streaming */
	{ .dscp = 31, .msc = MBUF_SC_BE },              /*  */

	{ .dscp = _DSCP_CS4, .msc = MBUF_SC_VI },       /* RFC 2474 Real-Time Interactive */
	{ .dscp = 33, .msc = MBUF_SC_VI },              /*  */
	{ .dscp = _DSCP_AF41, .msc = MBUF_SC_VI },      /* RFC 2597 Multimedia Conferencing */
	{ .dscp = 35, .msc = MBUF_SC_VI },              /*  */
	{ .dscp = _DSCP_AF42, .msc = MBUF_SC_VI },      /* RFC 2597 Multimedia Conferencing */
	{ .dscp = 37, .msc = MBUF_SC_VI },              /*  */
	{ .dscp = _DSCP_AF43, .msc = MBUF_SC_VI },      /* RFC 2597 Multimedia Conferencing */
	{ .dscp = 39, .msc = MBUF_SC_VI },              /*  */

	{ .dscp = _DSCP_CS5, .msc = MBUF_SC_VI },       /* RFC 2474 Signaling */
	{ .dscp = 41, .msc = MBUF_SC_VI },              /*  */
	{ .dscp = 42, .msc = MBUF_SC_VI },              /*  */
	{ .dscp = 43, .msc = MBUF_SC_VI },              /*  */
	{ .dscp = _DSCP_VA, .msc = MBUF_SC_VI },        /* RFC 5865 VOICE-ADMIT */
	{ .dscp = 45, .msc = MBUF_SC_VI },              /*  */
	{ .dscp = _DSCP_EF, .msc = MBUF_SC_VI },        /* RFC 3246 Telephony */
	{ .dscp = 47, .msc = MBUF_SC_VI },              /*  */

	{ .dscp = _DSCP_CS6, .msc = MBUF_SC_VO },       /* Wi-Fi WMM Certification: Chariot */
	{ .dscp = 49, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 50, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 51, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 52, .msc = MBUF_SC_VO },              /* Wi-Fi WMM Certification: Sigma */
	{ .dscp = 53, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 54, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 55, .msc = MBUF_SC_VO },              /*  */

	{ .dscp = _DSCP_CS7, .msc = MBUF_SC_VO },       /* Wi-Fi WMM Certification: Chariot */
	{ .dscp = 57, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 58, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 59, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 60, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 61, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 62, .msc = MBUF_SC_VO },              /*  */
	{ .dscp = 63, .msc = MBUF_SC_VO },              /*  */

	{ .dscp = 255, .msc = MBUF_SC_UNSPEC }          /* invalid DSCP to mark last entry */
};

mbuf_svc_class_t wifi_dscp_to_msc_array[DSCP_ARRAY_SIZE];

/*
 * If there is no foreground activity on the interface for bg_switch_time
 * seconds, the background connections can switch to foreground TCP
 * congestion control.
 */
#define TCP_BG_SWITCH_TIME 2 /* seconds */

#if (DEVELOPMENT || DEBUG)

static LCK_GRP_DECLARE(tclass_lck_grp, "tclass");
static LCK_MTX_DECLARE(tclass_lock, &tclass_lck_grp);

static int tfp_count = 0;

static TAILQ_HEAD(, tclass_for_proc) tfp_head =
    TAILQ_HEAD_INITIALIZER(tfp_head);

struct tclass_for_proc {
	TAILQ_ENTRY(tclass_for_proc)    tfp_link;
	int             tfp_class;
	pid_t           tfp_pid;
	char            tfp_pname[(2 * MAXCOMLEN) + 1];
	uint32_t        tfp_qos_mode;
};

static int get_pid_tclass(struct so_tcdbg *);
static int get_pname_tclass(struct so_tcdbg *);
static int set_pid_tclass(struct so_tcdbg *);
static int set_pname_tclass(struct so_tcdbg *);
static int flush_pid_tclass(struct so_tcdbg *);
static int purge_tclass_for_proc(void);
static int flush_tclass_for_proc(void);
static void set_tclass_for_curr_proc(struct socket *);

/*
 * Must be called with tclass_lock held
 */
static struct tclass_for_proc *
find_tfp_by_pid(pid_t pid)
{
	struct tclass_for_proc *tfp;

	TAILQ_FOREACH(tfp, &tfp_head, tfp_link) {
		if (tfp->tfp_pid == pid) {
			break;
		}
	}
	return tfp;
}

/*
 * Must be called with tclass_lock held
 */
static struct tclass_for_proc *
find_tfp_by_pname(const char *pname)
{
	struct tclass_for_proc *tfp;

	TAILQ_FOREACH(tfp, &tfp_head, tfp_link) {
		if (strlcmp(tfp->tfp_pname, pname,
		    sizeof(tfp->tfp_pname)) == 0) {
			break;
		}
	}
	return tfp;
}

__private_extern__ void
set_tclass_for_curr_proc(struct socket *so)
{
	struct tclass_for_proc *tfp = NULL;
	proc_t p = current_proc();      /* Not ref counted */
	pid_t pid = proc_pid(p);
	const char *__null_terminated pname = proc_best_name(p);

	lck_mtx_lock(&tclass_lock);

	TAILQ_FOREACH(tfp, &tfp_head, tfp_link) {
		if ((tfp->tfp_pid == pid) || (tfp->tfp_pid == -1 &&
		    strlcmp(tfp->tfp_pname, pname,
		    sizeof(tfp->tfp_pname)) == 0)) {
			if (tfp->tfp_class != SO_TC_UNSPEC) {
				so->so_traffic_class = (uint16_t)tfp->tfp_class;
			}

			if (tfp->tfp_qos_mode == QOS_MODE_MARKING_POLICY_ENABLE) {
				so->so_flags1 |= SOF1_QOSMARKING_ALLOWED;
			} else if (tfp->tfp_qos_mode == QOS_MODE_MARKING_POLICY_DISABLE) {
				so->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED;
			}
			break;
		}
	}

	lck_mtx_unlock(&tclass_lock);
}

/*
 * Purge entries with PIDs of exited processes
 */
int
purge_tclass_for_proc(void)
{
	int error = 0;
	struct tclass_for_proc *tfp, *tvar;

	lck_mtx_lock(&tclass_lock);

	TAILQ_FOREACH_SAFE(tfp, &tfp_head, tfp_link, tvar) {
		proc_t p;

		if (tfp->tfp_pid == -1) {
			continue;
		}
		if ((p = proc_find(tfp->tfp_pid)) == NULL) {
			tfp_count--;
			TAILQ_REMOVE(&tfp_head, tfp, tfp_link);

			kfree_type(struct tclass_for_proc, tfp);
		} else {
			proc_rele(p);
		}
	}

	lck_mtx_unlock(&tclass_lock);

	return error;
}

/*
 * Remove one entry
 * Must be called with tclass_lock held
 */
static void
free_tclass_for_proc(struct tclass_for_proc *tfp)
{
	if (tfp == NULL) {
		return;
	}
	tfp_count--;
	TAILQ_REMOVE(&tfp_head, tfp, tfp_link);
	kfree_type(struct tclass_for_proc, tfp);
}

/*
 * Remove all entries
 */
int
flush_tclass_for_proc(void)
{
	int error = 0;
	struct tclass_for_proc *tfp, *tvar;

	lck_mtx_lock(&tclass_lock);

	TAILQ_FOREACH_SAFE(tfp, &tfp_head, tfp_link, tvar) {
		free_tclass_for_proc(tfp);
	}

	lck_mtx_unlock(&tclass_lock);

	return error;
}

/*
 * Must be called with tclass_lock held
 */
static struct tclass_for_proc *
alloc_tclass_for_proc(pid_t pid, const char *__sized_by(pnamelen) pname, size_t pnamelen)
{
	struct tclass_for_proc *tfp;

	if (pid == -1 && pname == NULL) {
		return NULL;
	}

	tfp = kalloc_type(struct tclass_for_proc, Z_NOWAIT | Z_ZERO);
	if (tfp == NULL) {
		return NULL;
	}

	tfp->tfp_pid = pid;
	/*
	 * Add per pid entries before per proc name so we can find
	 * a specific instance of a process before the general name base entry.
	 */
	if (pid != -1) {
		TAILQ_INSERT_HEAD(&tfp_head, tfp, tfp_link);
	} else {
		if (pname != NULL) {
			strbufcpy(tfp->tfp_pname, sizeof(tfp->tfp_pname),
			    pname, pnamelen);
		} else {
			tfp->tfp_pname[0] = '\0';
		}
		TAILQ_INSERT_TAIL(&tfp_head, tfp, tfp_link);
	}

	tfp_count++;

	return tfp;
}

/*
 * SO_TC_UNSPEC for tclass means to remove the entry
 */
int
set_pid_tclass(struct so_tcdbg *so_tcdbg)
{
	int error = EINVAL;
	proc_t p = NULL;
	struct tclass_for_proc *tfp;
	pid_t pid = so_tcdbg->so_tcdbg_pid;
	int tclass = so_tcdbg->so_tcdbg_tclass;
	int netsvctype = so_tcdbg->so_tcdbg_netsvctype;
	uint8_t ecn_val = so_tcdbg->so_tcdbg_ecn_val;

	p = proc_find(pid);
	if (p == NULL) {
		printf("%s proc_find(%d) failed\n", __func__, pid);
		goto done;
	}

	/* Need a tfp */
	lck_mtx_lock(&tclass_lock);

	tfp = find_tfp_by_pid(pid);
	if (tfp == NULL) {
		tfp = alloc_tclass_for_proc(pid, NULL, 0);
		if (tfp == NULL) {
			error = ENOBUFS;
			goto done_unlock;
		}
	}
	tfp->tfp_class = tclass;
	tfp->tfp_qos_mode = so_tcdbg->so_tcbbg_qos_mode;

	if (tfp != NULL) {
		struct fileproc *fp;
		proc_fdlock(p);
		fdt_foreach(fp, p) {
			struct socket *so;

			if (FILEGLOB_DTYPE(fp->fp_glob) != DTYPE_SOCKET) {
				continue;
			}

			so = (struct socket *)fp_get_data(fp);
			if (SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6) {
				continue;
			}

			socket_lock(so, 1);
			if (tfp->tfp_qos_mode == QOS_MODE_MARKING_POLICY_ENABLE) {
				so->so_flags1 |= SOF1_QOSMARKING_ALLOWED;
			} else if (tfp->tfp_qos_mode == QOS_MODE_MARKING_POLICY_DISABLE) {
				so->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED;
			}

			struct inpcb *inp = so ? sotoinpcb(so) : NULL;
			struct tcpcb *tp = inp ? intotcpcb(inp) : NULL;

			if (tp != NULL) {
				if (ecn_val == IPTOS_ECN_ECT1 || ecn_val == IPTOS_ECN_ECT0) {
					tp->ecn_flags |= (ecn_val == IPTOS_ECN_ECT1) ?
					    TE_FORCE_ECT1 : TE_FORCE_ECT0;
				} else {
					tp->ecn_flags &= ~(TE_FORCE_ECT1 | TE_FORCE_ECT0);
				}
			}
			socket_unlock(so, 1);

			if (netsvctype != _NET_SERVICE_TYPE_UNSPEC) {
				error = sock_setsockopt(so, SOL_SOCKET,
				    SO_NET_SERVICE_TYPE, &netsvctype, sizeof(int));
			}
			if (tclass != SO_TC_UNSPEC) {
				error = sock_setsockopt(so, SOL_SOCKET,
				    SO_TRAFFIC_CLASS, &tclass, sizeof(int));
			}
		}

		proc_fdunlock(p);
	}

	error = 0;
done_unlock:
	lck_mtx_unlock(&tclass_lock);
done:
	if (p != NULL) {
		proc_rele(p);
	}

	return error;
}

int
set_pname_tclass(struct so_tcdbg *so_tcdbg)
{
	int error = EINVAL;
	struct tclass_for_proc *tfp;

	lck_mtx_lock(&tclass_lock);

	tfp = find_tfp_by_pname(__unsafe_null_terminated_from_indexable(so_tcdbg->so_tcdbg_pname));
	if (tfp == NULL) {
		tfp = alloc_tclass_for_proc(-1, so_tcdbg->so_tcdbg_pname,
		    sizeof(so_tcdbg->so_tcdbg_pname));
		if (tfp == NULL) {
			lck_mtx_unlock(&tclass_lock);
			error = ENOBUFS;
			goto done;
		}
	}
	tfp->tfp_class = so_tcdbg->so_tcdbg_tclass;
	tfp->tfp_qos_mode = so_tcdbg->so_tcbbg_qos_mode;

	lck_mtx_unlock(&tclass_lock);

	error = 0;
done:

	return error;
}

static int
flush_pid_tclass(struct so_tcdbg *so_tcdbg)
{
	pid_t pid = so_tcdbg->so_tcdbg_pid;
	int tclass = so_tcdbg->so_tcdbg_tclass;
	struct fileproc *fp;
	proc_t p;
	int error;

	p = proc_find(pid);
	if (p == PROC_NULL) {
		printf("%s proc_find(%d) failed\n", __func__, pid);
		return EINVAL;
	}

	proc_fdlock(p);

	fdt_foreach(fp, p) {
		struct socket *so;

		if (FILEGLOB_DTYPE(fp->fp_glob) != DTYPE_SOCKET) {
			continue;
		}

		so = (struct socket *)fp_get_data(fp);
		error = sock_setsockopt(so, SOL_SOCKET, SO_FLUSH, &tclass,
		    sizeof(tclass));
		if (error != 0) {
			printf("%s: setsockopt(SO_FLUSH) (so=0x%llx, fd=%d, "
			    "tclass=%d) failed %d\n", __func__,
			    (uint64_t)VM_KERNEL_ADDRPERM(so), fdt_foreach_fd(), tclass,
			    error);
		}
	}

	proc_fdunlock(p);

	proc_rele(p);
	return 0;
}

int
get_pid_tclass(struct so_tcdbg *so_tcdbg)
{
	int error = EINVAL;
	proc_t p = NULL;
	struct tclass_for_proc *tfp;
	pid_t pid = so_tcdbg->so_tcdbg_pid;

	so_tcdbg->so_tcdbg_tclass = SO_TC_UNSPEC; /* Means not set */

	p = proc_find(pid);
	if (p == NULL) {
		printf("%s proc_find(%d) failed\n", __func__, pid);
		goto done;
	}

	/* Need a tfp */
	lck_mtx_lock(&tclass_lock);

	tfp = find_tfp_by_pid(pid);
	if (tfp != NULL) {
		so_tcdbg->so_tcdbg_tclass = tfp->tfp_class;
		so_tcdbg->so_tcbbg_qos_mode = tfp->tfp_qos_mode;
		error = 0;
	}
	lck_mtx_unlock(&tclass_lock);
done:
	if (p != NULL) {
		proc_rele(p);
	}

	return error;
}

int
get_pname_tclass(struct so_tcdbg *so_tcdbg)
{
	int error = EINVAL;
	struct tclass_for_proc *tfp;

	so_tcdbg->so_tcdbg_tclass = SO_TC_UNSPEC; /* Means not set */

	/* Need a tfp */
	lck_mtx_lock(&tclass_lock);

	tfp = find_tfp_by_pname(__unsafe_null_terminated_from_indexable(so_tcdbg->so_tcdbg_pname));
	if (tfp != NULL) {
		so_tcdbg->so_tcdbg_tclass = tfp->tfp_class;
		so_tcdbg->so_tcbbg_qos_mode = tfp->tfp_qos_mode;
		error = 0;
	}
	lck_mtx_unlock(&tclass_lock);

	return error;
}

static int
delete_tclass_for_pid_pname(struct so_tcdbg *so_tcdbg)
{
	int error = EINVAL;
	pid_t pid = so_tcdbg->so_tcdbg_pid;
	struct tclass_for_proc *tfp = NULL;

	lck_mtx_lock(&tclass_lock);

	if (pid != -1) {
		tfp = find_tfp_by_pid(pid);
	} else {
		tfp = find_tfp_by_pname(__unsafe_null_terminated_from_indexable(so_tcdbg->so_tcdbg_pname));
	}

	if (tfp != NULL) {
		free_tclass_for_proc(tfp);
		error = 0;
	}

	lck_mtx_unlock(&tclass_lock);

	return error;
}

/*
 * Setting options requires privileges
 */
__private_extern__ int
so_set_tcdbg(struct socket *so, struct so_tcdbg *so_tcdbg)
{
	int error = 0;

	if ((so->so_state & SS_PRIV) == 0) {
		return EPERM;
	}

	socket_unlock(so, 0);

	switch (so_tcdbg->so_tcdbg_cmd) {
	case SO_TCDBG_PID:
		error = set_pid_tclass(so_tcdbg);
		break;

	case SO_TCDBG_PNAME:
		error = set_pname_tclass(so_tcdbg);
		break;

	case SO_TCDBG_PURGE:
		error = purge_tclass_for_proc();
		break;

	case SO_TCDBG_FLUSH:
		error = flush_tclass_for_proc();
		break;

	case SO_TCDBG_DELETE:
		error = delete_tclass_for_pid_pname(so_tcdbg);
		break;

	case SO_TCDBG_TCFLUSH_PID:
		error = flush_pid_tclass(so_tcdbg);
		break;

	default:
		error = EINVAL;
		break;
	}

	socket_lock(so, 0);

	return error;
}

/*
 * Not required to be privileged to get
 */
__private_extern__ int
sogetopt_tcdbg(struct socket *so, struct sockopt *sopt)
{
	int error = 0;
	struct so_tcdbg so_tcdbg;
	void *buf = NULL;
	size_t len = sopt->sopt_valsize;

	error = sooptcopyin(sopt, &so_tcdbg, sizeof(struct so_tcdbg),
	    sizeof(struct so_tcdbg));
	if (error != 0) {
		return error;
	}

	sopt->sopt_valsize = len;

	socket_unlock(so, 0);

	switch (so_tcdbg.so_tcdbg_cmd) {
	case SO_TCDBG_PID:
		error = get_pid_tclass(&so_tcdbg);
		break;

	case SO_TCDBG_PNAME:
		error = get_pname_tclass(&so_tcdbg);
		break;

	case SO_TCDBG_COUNT:
		lck_mtx_lock(&tclass_lock);
		so_tcdbg.so_tcdbg_count = tfp_count;
		lck_mtx_unlock(&tclass_lock);
		break;

	case SO_TCDBG_LIST: {
		struct tclass_for_proc *tfp;
		int n, alloc_count;
		struct so_tcdbg *ptr;

		lck_mtx_lock(&tclass_lock);
		if ((alloc_count = tfp_count) == 0) {
			lck_mtx_unlock(&tclass_lock);
			error = EINVAL;
			break;
		}
		len = alloc_count * sizeof(struct so_tcdbg);
		lck_mtx_unlock(&tclass_lock);

		buf = kalloc_data(len, Z_WAITOK | Z_ZERO);
		if (buf == NULL) {
			error = ENOBUFS;
			break;
		}

		lck_mtx_lock(&tclass_lock);
		n = 0;
		ptr = (struct so_tcdbg *)buf;
		TAILQ_FOREACH(tfp, &tfp_head, tfp_link) {
			if (++n > alloc_count) {
				break;
			}
			if (tfp->tfp_pid != -1) {
				ptr->so_tcdbg_cmd = SO_TCDBG_PID;
				ptr->so_tcdbg_pid = tfp->tfp_pid;
			} else {
				ptr->so_tcdbg_cmd = SO_TCDBG_PNAME;
				ptr->so_tcdbg_pid = -1;
				strbufcpy(ptr->so_tcdbg_pname,
				    tfp->tfp_pname);
			}
			ptr->so_tcdbg_tclass = tfp->tfp_class;
			ptr->so_tcbbg_qos_mode = tfp->tfp_qos_mode;
			ptr++;
		}

		lck_mtx_unlock(&tclass_lock);
	}
	break;

	default:
		error = EINVAL;
		break;
	}

	socket_lock(so, 0);

	if (error == 0) {
		if (buf == NULL) {
			error = sooptcopyout(sopt, &so_tcdbg,
			    sizeof(struct so_tcdbg));
		} else {
			error = sooptcopyout(sopt, buf, len);
			kfree_data(buf, len);
		}
	}
	return error;
}

#endif /* (DEVELOPMENT || DEBUG) */

int
so_get_netsvc_marking_level(struct socket *so)
{
	int marking_level = NETSVC_MRKNG_UNKNOWN;
	struct ifnet *ifp = NULL;

	switch (SOCK_DOM(so)) {
	case PF_INET: {
		struct inpcb *inp = sotoinpcb(so);

		if (inp != NULL) {
			ifp = inp->inp_last_outifp;
		}
		break;
	}
	case PF_INET6: {
		struct in6pcb *in6p = sotoin6pcb(so);

		if (in6p != NULL) {
			ifp = in6p->in6p_last_outifp;
		}
		break;
	}
	default:
		break;
	}
	if (ifp != NULL) {
		if ((ifp->if_eflags & IFEF_QOSMARKING_ENABLED) != 0) {
			if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
				marking_level = NETSVC_MRKNG_LVL_L3L2_ALL;
			} else {
				marking_level = NETSVC_MRKNG_LVL_L3L2_BK;
			}
		} else {
			marking_level = NETSVC_MRKNG_LVL_L2;
		}
	}
	return marking_level;
}

__private_extern__ int
so_set_traffic_class(struct socket *so, int optval)
{
	int error = 0;

	if (optval < SO_TC_BE || optval > SO_TC_CTL) {
		error = EINVAL;
	} else {
		switch (optval) {
		case _SO_TC_BK:
			optval = SO_TC_BK;
			break;
		case _SO_TC_VI:
			optval = SO_TC_VI;
			break;
		case _SO_TC_VO:
			optval = SO_TC_VO;
			break;
		default:
			if (!SO_VALID_TC(optval)) {
				error = EINVAL;
			}
			break;
		}

		if (error == 0) {
			int oldval = so->so_traffic_class;

			VERIFY(SO_VALID_TC(optval));
			so->so_traffic_class = (uint16_t)optval;

			if ((SOCK_DOM(so) == PF_INET ||
			    SOCK_DOM(so) == PF_INET6) &&
			    SOCK_TYPE(so) == SOCK_STREAM) {
				set_tcp_stream_priority(so);
			}

			if ((SOCK_DOM(so) == PF_INET ||
			    SOCK_DOM(so) == PF_INET6) &&
			    optval != oldval && (optval == SO_TC_BK_SYS ||
			    oldval == SO_TC_BK_SYS)) {
				/*
				 * If the app switches from BK_SYS to something
				 * else, resume the socket if it was suspended.
				 */
				if (oldval == SO_TC_BK_SYS) {
					inp_reset_fc_state(so->so_pcb);
				}

				SOTHROTTLELOG("throttle[%d]: so 0x%llx "
				    "[%d,%d] opportunistic %s\n", so->last_pid,
				    (uint64_t)VM_KERNEL_ADDRPERM(so),
				    SOCK_DOM(so), SOCK_TYPE(so),
				    (optval == SO_TC_BK_SYS) ? "ON" : "OFF");
			}
		}
	}
	return error;
}

__private_extern__ int
so_set_net_service_type(struct socket *so, int netsvctype)
{
	int sotc;
	int error;

	if (!IS_VALID_NET_SERVICE_TYPE(netsvctype)) {
		return EINVAL;
	}

	sotc = sotc_by_netservicetype[netsvctype];
	error = so_set_traffic_class(so, sotc);
	if (error != 0) {
		return error;
	}
	so->so_netsvctype = (int8_t)netsvctype;
	so->so_flags1 |= SOF1_TC_NET_SERV_TYPE;

	return 0;
}

__private_extern__ void
so_set_default_traffic_class(struct socket *so)
{
	so->so_traffic_class = SO_TC_BE;

	if ((SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6)) {
		if (net_qos_policy_restricted == 0) {
			so->so_flags1 |= SOF1_QOSMARKING_ALLOWED;
		}
#if (DEVELOPMENT || DEBUG)
		if (tfp_count > 0) {
			set_tclass_for_curr_proc(so);
		}
#endif /* (DEVELOPMENT || DEBUG) */
	}
}

__private_extern__ int
so_set_opportunistic(struct socket *so, int optval)
{
	return so_set_traffic_class(so, (optval == 0) ?
	           SO_TC_BE : SO_TC_BK_SYS);
}

__private_extern__ int
so_get_opportunistic(struct socket *so)
{
	return so->so_traffic_class == SO_TC_BK_SYS;
}

__private_extern__ int
ip_tos_from_control(struct mbuf *control)
{
	struct cmsghdr *cm;
	int tos = IPTOS_UNSPEC;

	for (cm = M_FIRST_CMSGHDR(control);
	    is_cmsg_valid(control, cm);
	    cm = M_NXT_CMSGHDR(control, cm)) {
		if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
			continue;
		}

		if ((cm->cmsg_level == IPPROTO_IP &&
		    cm->cmsg_type == IP_TOS) ||
		    (cm->cmsg_level == IPPROTO_IPV6 &&
		    cm->cmsg_type == IPV6_TCLASS)) {
			tos = *(int *)(void *)CMSG_DATA(cm) & IPTOS_MASK;
			/* The first valid option wins */
			break;
		}
	}

	return tos;
}

/*
 * There is no traffic class for input packet
 */
__private_extern__ void
so_recv_data_stat(struct socket *so, struct mbuf *m, size_t off)
{
	so->so_tc_stats[SO_STATS_DATA].rxpackets += 1;
	so->so_tc_stats[SO_STATS_DATA].rxbytes +=
	    ((m->m_flags & M_PKTHDR) ? m->m_pkthdr.len : 0) + off;
}

__private_extern__ void
so_inc_recv_data_stat(struct socket *so, size_t pkts, size_t bytes)
{
	so->so_tc_stats[SO_STATS_DATA].rxpackets += pkts;
	so->so_tc_stats[SO_STATS_DATA].rxbytes += bytes;
}

static inline int
so_throttle_best_effort(struct socket *so, struct ifnet *ifp)
{
	uint32_t uptime = (uint32_t)net_uptime();
	return soissrcbesteffort(so) &&
	       net_io_policy_throttle_best_effort == 1 &&
	       ifp->if_rt_sendts > 0 &&
	       (int)(uptime - ifp->if_rt_sendts) <= TCP_BG_SWITCH_TIME;
}

__private_extern__ void
set_tcp_stream_priority(struct socket *so)
{
	struct inpcb *inp = sotoinpcb(so);
	struct tcpcb *tp = intotcpcb(inp);
	struct ifnet *outifp;
	u_char old_cc = tp->tcp_cc_index;
	int recvbg = IS_TCP_RECV_BG(so);
	bool is_local = false, fg_active = false;
	uint32_t uptime;

	VERIFY((SOCK_CHECK_DOM(so, PF_INET) ||
	    SOCK_CHECK_DOM(so, PF_INET6)) &&
	    SOCK_CHECK_TYPE(so, SOCK_STREAM) &&
	    SOCK_CHECK_PROTO(so, IPPROTO_TCP));

	/* Return if the socket is in a terminal state */
	if (inp->inp_state == INPCB_STATE_DEAD) {
		return;
	}

	outifp = inp->inp_last_outifp;
	uptime = (uint32_t)net_uptime();

	/*
	 * If the socket was marked as a background socket or if the
	 * traffic class is set to background with traffic class socket
	 * option then make both send and recv side of the stream to be
	 * background. The variable sotcdb which can be set with sysctl
	 * is used to disable these settings for testing.
	 */
	if (outifp == NULL || (outifp->if_flags & IFF_LOOPBACK)) {
		is_local = true;
	}

	/* Check if there has been recent foreground activity */
	if (outifp != NULL) {
		/*
		 * If the traffic source is background, check if
		 * there is recent foreground activity which should
		 * continue to keep the traffic source as background.
		 * Otherwise, we can switch the traffic source to
		 * foreground.
		 */
		if (soissrcbackground(so) && outifp->if_fg_sendts > 0 &&
		    (int)(uptime - outifp->if_fg_sendts) <= TCP_BG_SWITCH_TIME) {
			fg_active = true;
		}

		/*
		 * The traffic source is best-effort -- check if
		 * the policy to throttle best effort is enabled
		 * and there was realtime activity on this
		 * interface recently. If this is true, enable
		 * algorithms that respond to increased latency
		 * on best-effort traffic.
		 */
		if (so_throttle_best_effort(so, outifp)) {
			fg_active = true;
		}
	}

	/*
	 * System initiated background traffic like cloud uploads should
	 * always use background delay sensitive algorithms. This will
	 * make the stream more responsive to other streams on the user's
	 * network and it will minimize latency induced.
	 */
	if (fg_active || IS_SO_TC_BACKGROUNDSYSTEM(so->so_traffic_class)) {
		/*
		 * If the interface that the connection is using is
		 * loopback, do not use background congestion
		 * control algorithm.
		 *
		 * If there has been recent foreground activity or if there
		 * was an indication that a real time foreground application
		 * is going to use networking (net_io_policy_throttled),
		 * switch the background and best effort streams to use background
		 * congestion control algorithm.
		 */
		if ((sotcdb & SOTCDB_NO_SENDTCPBG) != 0 || is_local) {
			if (old_cc == TCP_CC_ALGO_BACKGROUND_INDEX) {
				tcp_set_foreground_cc(so);
			}
		} else {
			if (old_cc != TCP_CC_ALGO_BACKGROUND_INDEX) {
				tcp_set_background_cc(so);
			}
		}

		/* Set receive side background flags */
		if ((sotcdb & SOTCDB_NO_RECVTCPBG) != 0 || is_local) {
			tcp_clear_recv_bg(so);
		} else {
			tcp_set_recv_bg(so);
		}
	} else {
		/*
		 * If there is no recent foreground activity, even the
		 * background flows can use foreground congestion controller.
		 */
		tcp_clear_recv_bg(so);
		if (old_cc == TCP_CC_ALGO_BACKGROUND_INDEX) {
			tcp_set_foreground_cc(so);
		}
	}

	if (old_cc != tp->tcp_cc_index || recvbg != IS_TCP_RECV_BG(so)) {
		SOTHROTTLELOG("throttle[%d]: so 0x%llx [%d,%d] TCP %s send; "
		    "%s recv\n", so->last_pid,
		    (uint64_t)VM_KERNEL_ADDRPERM(so),
		    SOCK_DOM(so), SOCK_TYPE(so),
		    (tp->tcp_cc_index == TCP_CC_ALGO_BACKGROUND_INDEX) ?
		    "background" : "foreground",
		    IS_TCP_RECV_BG(so) ? "background" : "foreground");
	}
}

/*
 * Set traffic class to an IPv4 or IPv6 packet
 * - mark the mbuf
 * - set the DSCP code following the WMM mapping
 */
__private_extern__ void
set_packet_service_class(struct mbuf *m, struct socket *so,
    int sotc, uint32_t flags)
{
	mbuf_svc_class_t msc = MBUF_SC_BE;         /* Best effort by default */
	struct inpcb *inp = sotoinpcb(so); /* in6pcb and inpcb are the same */

	if (!(m->m_flags & M_PKTHDR)) {
		return;
	}

	/*
	 * Here is the precedence:
	 * 1) TRAFFIC_MGT_SO_BACKGROUND trumps all
	 * 2) Traffic class passed via ancillary data to sendmsdg(2)
	 * 3) Traffic class socket option last
	 */
	if (sotc != SO_TC_UNSPEC) {
		VERIFY(SO_VALID_TC(sotc));
		msc = so_tc2msc(sotc);
		/* Assert because tc must have been valid */
		VERIFY(MBUF_VALID_SC(msc));
	}

	/*
	 * If TRAFFIC_MGT_SO_BACKGROUND is set or policy to throttle
	 * best effort is set, depress the priority.
	 */
	if (!IS_MBUF_SC_BACKGROUND(msc) && soisthrottled(so)) {
		msc = MBUF_SC_BK;
	}

	if (IS_MBUF_SC_BESTEFFORT(msc) && inp->inp_last_outifp != NULL &&
	    so_throttle_best_effort(so, inp->inp_last_outifp)) {
		msc = MBUF_SC_BK;
	}

	if (soissrcbackground(so)) {
		m->m_pkthdr.pkt_flags |= PKTF_SO_BACKGROUND;
	}

	if (soissrcrealtime(so) || IS_MBUF_SC_REALTIME(msc)) {
		m->m_pkthdr.pkt_flags |= PKTF_SO_REALTIME;
	}
	/*
	 * Set the traffic class in the mbuf packet header svc field
	 */
	if (sotcdb & SOTCDB_NO_MTC) {
		goto no_mbtc;
	}

	/*
	 * Elevate service class if the packet is a pure TCP ACK.
	 * We can do this only when the flow is not a background
	 * flow and the outgoing interface supports
	 * transmit-start model.
	 */
	if (!IS_MBUF_SC_BACKGROUND(msc) &&
	    (flags & (PKT_SCF_TCP_ACK | PKT_SCF_TCP_SYN)) != 0) {
		msc = MBUF_SC_CTL;
	}

	(void) m_set_service_class(m, msc);

	/*
	 * Set the privileged traffic auxiliary flag if applicable,
	 * or clear it.
	 */
	if (!(sotcdb & SOTCDB_NO_PRIVILEGED) && soisprivilegedtraffic(so) &&
	    msc != MBUF_SC_UNSPEC) {
		m->m_pkthdr.pkt_flags |= PKTF_PRIO_PRIVILEGED;
	} else {
		m->m_pkthdr.pkt_flags &= ~PKTF_PRIO_PRIVILEGED;
	}

no_mbtc:
	/*
	 * For TCP with background traffic class switch CC algo based on sysctl
	 */
	if (so->so_type == SOCK_STREAM) {
		set_tcp_stream_priority(so);
	}
}

__private_extern__ mbuf_svc_class_t
so_tc2msc(int tc)
{
	mbuf_svc_class_t msc;

	switch (tc) {
	case SO_TC_BK_SYS:
		msc = MBUF_SC_BK_SYS;
		break;
	case SO_TC_BK:
	case _SO_TC_BK:
		msc = MBUF_SC_BK;
		break;
	case SO_TC_BE:
		msc = MBUF_SC_BE;
		break;
	case SO_TC_RD:
		msc = MBUF_SC_RD;
		break;
	case SO_TC_OAM:
		msc = MBUF_SC_OAM;
		break;
	case SO_TC_AV:
		msc = MBUF_SC_AV;
		break;
	case SO_TC_RV:
		msc = MBUF_SC_RV;
		break;
	case SO_TC_VI:
	case _SO_TC_VI:
		msc = MBUF_SC_VI;
		break;
	case SO_TC_NETSVC_SIG:
		msc = MBUF_SC_SIG;
		break;
	case SO_TC_VO:
	case _SO_TC_VO:
		msc = MBUF_SC_VO;
		break;
	case SO_TC_CTL:
		msc = MBUF_SC_CTL;
		break;
	case SO_TC_ALL:
	default:
		msc = MBUF_SC_UNSPEC;
		break;
	}

	return msc;
}

__private_extern__ int
so_svc2tc(mbuf_svc_class_t svc)
{
	switch (svc) {
	case MBUF_SC_BK_SYS:
		return SO_TC_BK_SYS;
	case MBUF_SC_BK:
		return SO_TC_BK;
	case MBUF_SC_BE:
		return SO_TC_BE;
	case MBUF_SC_RD:
		return SO_TC_RD;
	case MBUF_SC_OAM:
		return SO_TC_OAM;
	case MBUF_SC_AV:
		return SO_TC_AV;
	case MBUF_SC_RV:
		return SO_TC_RV;
	case MBUF_SC_VI:
		return SO_TC_VI;
	case MBUF_SC_SIG:
		return SO_TC_NETSVC_SIG;
	case MBUF_SC_VO:
		return SO_TC_VO;
	case MBUF_SC_CTL:
		return SO_TC_CTL;
	case MBUF_SC_UNSPEC:
	default:
		return SO_TC_BE;
	}
}

static size_t
sotc_index(int sotc)
{
	switch (sotc) {
	case SO_TC_BK_SYS:
		return SOTCIX_BK_SYS;
	case _SO_TC_BK:
	case SO_TC_BK:
		return SOTCIX_BK;

	case SO_TC_BE:
		return SOTCIX_BE;
	case SO_TC_RD:
		return SOTCIX_RD;
	case SO_TC_OAM:
		return SOTCIX_OAM;

	case SO_TC_AV:
		return SOTCIX_AV;
	case SO_TC_RV:
		return SOTCIX_RV;
	case _SO_TC_VI:
	case SO_TC_VI:
		return SOTCIX_VI;

	case _SO_TC_VO:
	case SO_TC_VO:
		return SOTCIX_VO;
	case SO_TC_CTL:
		return SOTCIX_CTL;

	default:
		break;
	}
	/*
	 * Unknown traffic class value
	 */
	return SIZE_T_MAX;
}

uint8_t
fastlane_sc_to_dscp(uint32_t svc_class)
{
	uint8_t dscp = _DSCP_DF;

	switch (svc_class) {
	case MBUF_SC_BK_SYS:
	case MBUF_SC_BK:
		dscp = _DSCP_AF11;
		break;

	case MBUF_SC_BE:
		dscp = _DSCP_DF;
		break;
	case MBUF_SC_RD:
		dscp = _DSCP_AF21;
		break;
	case MBUF_SC_OAM:
		dscp = _DSCP_CS2;
		break;

	case MBUF_SC_AV:
		dscp = _DSCP_AF31;
		break;
	case MBUF_SC_RV:
		dscp = _DSCP_CS4;
		break;
	case MBUF_SC_VI:
		dscp = _DSCP_AF41;
		break;
	case MBUF_SC_SIG:
		dscp = _DSCP_CS3;
		break;

	case MBUF_SC_VO:
		dscp = _DSCP_EF;
		break;
	case MBUF_SC_CTL:
		dscp = _DSCP_DF;
		break;
	default:
		dscp = _DSCP_DF;
		break;
	}

	return dscp;
}

uint8_t
rfc4594_sc_to_dscp(uint32_t svc_class)
{
	uint8_t dscp = _DSCP_DF;

	switch (svc_class) {
	case MBUF_SC_BK_SYS:            /* Low-Priority Data */
	case MBUF_SC_BK:
		dscp = _DSCP_CS1;
		break;

	case MBUF_SC_BE:                /* Standard */
		dscp = _DSCP_DF;
		break;
	case MBUF_SC_RD:                /* Low-Latency Data */
		dscp = _DSCP_AF21;
		break;

	/* SVC_CLASS Not Defined:  High-Throughput Data */

	case MBUF_SC_OAM:               /* OAM */
		dscp = _DSCP_CS2;
		break;

	/* SVC_CLASS Not Defined:  Broadcast Video */

	case MBUF_SC_AV:                /* Multimedia Streaming */
		dscp = _DSCP_AF31;
		break;
	case MBUF_SC_RV:                /* Real-Time Interactive */
		dscp = _DSCP_CS4;
		break;
	case MBUF_SC_VI:                /* Multimedia Conferencing */
		dscp = _DSCP_AF41;
		break;
	case MBUF_SC_SIG:               /* Signaling */
		dscp = _DSCP_CS5;
		break;

	case MBUF_SC_VO:                /* Telephony */
		dscp = _DSCP_EF;
		break;
	case MBUF_SC_CTL:               /* Network Control*/
		dscp = _DSCP_CS6;
		break;
	default:
		dscp = _DSCP_DF;
		break;
	}

	return dscp;
}

mbuf_traffic_class_t
rfc4594_dscp_to_tc(uint8_t dscp)
{
	mbuf_traffic_class_t tc = MBUF_TC_BE;

	switch (dscp) {
	case _DSCP_CS1:
		tc = MBUF_TC_BK;
		break;
	case _DSCP_DF:
	case _DSCP_AF21:
	case _DSCP_CS2:
		tc = MBUF_TC_BE;
		break;
	case _DSCP_AF31:
	case _DSCP_CS4:
	case _DSCP_AF41:
	case _DSCP_CS5:
		tc = MBUF_TC_VI;
		break;
	case _DSCP_EF:
	case _DSCP_CS6:
		tc = MBUF_TC_VO;
		break;
	default:
		tc = MBUF_TC_BE;
		break;
	}

	return tc;
}

/*
 * Pass NULL ifp for default map
 */
static errno_t
set_netsvctype_dscp_map(struct net_qos_dscp_map *net_qos_dscp_map,
    const struct netsvctype_dscp_map *__counted_by(_NET_SERVICE_TYPE_COUNT) netsvctype_dscp_map)
{
	size_t i;
	int netsvctype;

	VERIFY(netsvctype_dscp_map != NULL);
	/*
	 * Do not accept more that max number of distinct DSCPs
	 */
	if (net_qos_dscp_map == NULL) {
		return EINVAL;
	}

	/*
	 * Validate input parameters
	 */
	for (i = 0; i < _NET_SERVICE_TYPE_COUNT; i++) {
		if (!IS_VALID_NET_SERVICE_TYPE(netsvctype_dscp_map[i].netsvctype)) {
			return EINVAL;
		}
		if (netsvctype_dscp_map[i].dscp > _MAX_DSCP) {
			return EINVAL;
		}
	}

	for (i = 0; i < _NET_SERVICE_TYPE_COUNT; i++) {
		netsvctype = netsvctype_dscp_map[i].netsvctype;

		net_qos_dscp_map->netsvctype_to_dscp[netsvctype] =
		    netsvctype_dscp_map[i].dscp;
	}
	for (netsvctype = 0; netsvctype < _NET_SERVICE_TYPE_COUNT; netsvctype++) {
		switch (netsvctype) {
		case NET_SERVICE_TYPE_BE:
		case NET_SERVICE_TYPE_BK:
		case NET_SERVICE_TYPE_VI:
		case NET_SERVICE_TYPE_VO:
		case NET_SERVICE_TYPE_RV:
		case NET_SERVICE_TYPE_AV:
		case NET_SERVICE_TYPE_OAM:
		case NET_SERVICE_TYPE_RD: {
			size_t sotcix;

			sotcix = sotc_index(sotc_by_netservicetype[netsvctype]);
			if (sotcix != SIZE_T_MAX) {
				net_qos_dscp_map->sotc_to_dscp[sotcix]  =
				    netsvctype_dscp_map[netsvctype].dscp;
			}
			break;
		}
		case  NET_SERVICE_TYPE_SIG:
			/* Signaling does not have its own traffic class */
			break;
		default:
			/* We should not be here */
			ASSERT(0);
		}
	}
	if (net_qos_dscp_map == &fastlane_net_qos_dscp_map) {
		/* Network control socket traffic class is always best effort for fastlane*/
		net_qos_dscp_map->sotc_to_dscp[SOTCIX_CTL] = _DSCP_DF;
	} else {
		net_qos_dscp_map->sotc_to_dscp[SOTCIX_CTL] = _DSCP_CS6;
	}

	/* Background system socket traffic class DSCP same as background */
	net_qos_dscp_map->sotc_to_dscp[SOTCIX_BK_SYS] =
	    net_qos_dscp_map->sotc_to_dscp[SOTCIX_BK];

	return 0;
}

static size_t
get_netsvctype_dscp_map(struct netsvctype_dscp_map *__counted_by(_NET_SERVICE_TYPE_COUNT) netsvctype_dscp_map)
{
	struct net_qos_dscp_map *net_qos_dscp_map;
	int i;

	net_qos_dscp_map = &fastlane_net_qos_dscp_map;

	for (i = 0; i < _NET_SERVICE_TYPE_COUNT; i++) {
		netsvctype_dscp_map[i].netsvctype = i;
		netsvctype_dscp_map[i].dscp = net_qos_dscp_map->netsvctype_to_dscp[i];
	}

	return i * sizeof(struct netsvctype_dscp_map);
}

void
net_qos_map_init()
{
	errno_t error;

	error = set_netsvctype_dscp_map(&fastlane_net_qos_dscp_map,
	    fastlane_netsvctype_dscp_map);
	ASSERT(error == 0);

	error = set_netsvctype_dscp_map(&rfc4594_net_qos_dscp_map,
	    rfc4594_netsvctype_dscp_map);
	ASSERT(error == 0);

#if (DEBUG || DEVELOPMENT)
	error = set_netsvctype_dscp_map(&custom_net_qos_dscp_map,
	    rfc4594_netsvctype_dscp_map);
	ASSERT(error == 0);

#endif /* (DEBUG || DEVELOPMENT) */

	set_dscp_to_wifi_ac_map(default_dscp_to_wifi_ac_map, 1);
}

int
sysctl_default_netsvctype_to_dscp_map SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg1, arg2)
	int error = 0;

	if (req->oldptr == USER_ADDR_NULL) {
		req->oldidx =
		    _NET_SERVICE_TYPE_COUNT * sizeof(struct netsvctype_dscp_map);
	} else if (req->oldlen > 0) {
		struct netsvctype_dscp_map netsvctype_dscp_map[_NET_SERVICE_TYPE_COUNT] = {};
		size_t len;

		len = get_netsvctype_dscp_map(netsvctype_dscp_map);

		error = SYSCTL_OUT(req, netsvctype_dscp_map,
		    MIN(len, req->oldlen));
		if (error != 0) {
			goto done;
		}
	}

	if (req->newptr != USER_ADDR_NULL) {
		error = EPERM;
	}
done:
	return error;
}

__private_extern__ errno_t
set_packet_qos(struct mbuf *m, struct ifnet *ifp, boolean_t qos_allowed,
    int sotc, int netsvctype, uint8_t *dscp_inout)
{
	if (ifp == NULL || dscp_inout == NULL) {
		return EINVAL;
	}

	if ((ifp->if_eflags & IFEF_QOSMARKING_ENABLED) != 0 &&
	    ifp->if_qosmarking_mode != IFRTYPE_QOSMARKING_MODE_NONE) {
		uint8_t dscp;
		const struct net_qos_dscp_map *net_qos_dscp_map = NULL;

		switch (ifp->if_qosmarking_mode) {
		case IFRTYPE_QOSMARKING_FASTLANE:
			net_qos_dscp_map = &fastlane_net_qos_dscp_map;
			break;
		case IFRTYPE_QOSMARKING_RFC4594:
			net_qos_dscp_map = &rfc4594_net_qos_dscp_map;
			break;
#if (DEBUG || DEVELOPMENT)
		case IFRTYPE_QOSMARKING_CUSTOM:
			net_qos_dscp_map = &custom_net_qos_dscp_map;
			break;
#endif /* (DEBUG || DEVELOPMENT) */
		default:
			panic("invalid QoS marking type");
			/* NOTREACHED */
		}

		/*
		 * When on a Fastlane network, IP_TOS/IPV6_TCLASS are no-ops
		 */
		dscp = _DSCP_DF;

		/*
		 * For DSCP use the network service type is specified, otherwise
		 * use the socket traffic class
		 *
		 * When not whitelisted by the policy, set DSCP only for best
		 * effort and background, and set the mbuf service class to
		 * best effort as well so the packet will be queued and
		 * scheduled at a lower priority.
		 * We still want to prioritize control traffic on the interface
		 * so we do not change the mbuf service class for SO_TC_CTL
		 */
		if (IS_VALID_NET_SERVICE_TYPE(netsvctype) &&
		    netsvctype != NET_SERVICE_TYPE_BE) {
			dscp = net_qos_dscp_map->netsvctype_to_dscp[netsvctype];

			if (qos_allowed == FALSE &&
			    netsvctype != NET_SERVICE_TYPE_BE &&
			    netsvctype != NET_SERVICE_TYPE_BK) {
				dscp = _DSCP_DF;
				if (sotc != SO_TC_CTL) {
					m_set_service_class(m, MBUF_SC_BE);
				}
			}
		} else if (sotc != SO_TC_UNSPEC) {
			size_t sotcix = sotc_index(sotc);
			if (sotcix != SIZE_T_MAX) {
				dscp = net_qos_dscp_map->sotc_to_dscp[sotcix];

				if (qos_allowed == FALSE && sotc != SO_TC_BE &&
				    sotc != SO_TC_BK && sotc != SO_TC_BK_SYS &&
				    sotc != SO_TC_CTL) {
					dscp = _DSCP_DF;
					if (sotc != SO_TC_CTL) {
						m_set_service_class(m, MBUF_SC_BE);
					}
				}
			}
		}
		if (net_qos_verbose != 0) {
			printf("%s qos_allowed %d sotc %u netsvctype %u dscp %u\n",
			    __func__, qos_allowed, sotc, netsvctype, dscp);
		}

		if (*dscp_inout != dscp) {
			*dscp_inout = dscp;
		}
	} else if (*dscp_inout != _DSCP_DF && IFNET_IS_WIFI_INFRA(ifp)) {
		mbuf_svc_class_t msc = m_get_service_class(m);

		/*
		 * For WiFi infra, when the mbuf service class is best effort
		 * and the DSCP is not default, set the service class based
		 * on DSCP
		 */
		if (msc == MBUF_SC_BE) {
			msc = wifi_dscp_to_msc_array[*dscp_inout];

			if (msc != MBUF_SC_BE) {
				m_set_service_class(m, msc);

				if (net_qos_verbose != 0) {
					printf("%s set msc %u for dscp %u\n",
					    __func__, msc, *dscp_inout);
				}
			}
		}
	}

	return 0;
}

static void
set_dscp_to_wifi_ac_map(const struct dcsp_msc_map *__indexable map, int clear)
{
	int i;

	if (clear) {
		bzero(wifi_dscp_to_msc_array, sizeof(wifi_dscp_to_msc_array));
	}

	for (i = 0; i < DSCP_ARRAY_SIZE; i++) {
		const struct dcsp_msc_map *elem = map + i;

		if (elem->dscp > _MAX_DSCP || elem->msc == MBUF_SC_UNSPEC) {
			break;
		}
		switch (elem->msc) {
		case MBUF_SC_BK_SYS:
		case MBUF_SC_BK:
			wifi_dscp_to_msc_array[elem->dscp] = MBUF_SC_BK;
			break;
		default:
		case MBUF_SC_BE:
		case MBUF_SC_RD:
		case MBUF_SC_OAM:
			wifi_dscp_to_msc_array[elem->dscp] = MBUF_SC_BE;
			break;
		case MBUF_SC_AV:
		case MBUF_SC_RV:
		case MBUF_SC_VI:
			wifi_dscp_to_msc_array[elem->dscp] = MBUF_SC_VI;
			break;
		case MBUF_SC_VO:
		case MBUF_SC_CTL:
			wifi_dscp_to_msc_array[elem->dscp] = MBUF_SC_VO;
			break;
		}
	}
}

static errno_t
dscp_msc_map_from_netsvctype_dscp_map(struct netsvctype_dscp_map *__counted_by(count) netsvctype_dscp_map,
    size_t count, struct dcsp_msc_map *__counted_by(DSCP_ARRAY_SIZE) dcsp_msc_map)
{
	errno_t error = 0;
	uint32_t i;

	/*
	 * Validate input parameters
	 */
	for (i = 0; i < count; i++) {
		if (!SO_VALID_TC(netsvctype_dscp_map[i].netsvctype)) {
			error = EINVAL;
			goto done;
		}
		if (netsvctype_dscp_map[i].dscp > _MAX_DSCP) {
			error = EINVAL;
			goto done;
		}
	}

	bzero(dcsp_msc_map, DSCP_ARRAY_SIZE * sizeof(struct dcsp_msc_map));

	for (i = 0; i < count; i++) {
		dcsp_msc_map[i].dscp = netsvctype_dscp_map[i].dscp;
		dcsp_msc_map[i].msc = so_tc2msc(netsvctype_dscp_map[i].netsvctype);
	}
done:
	return error;
}

int
sysctl_dscp_to_wifi_ac_map SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg1, arg2)
	int error = 0;
	size_t len = DSCP_ARRAY_SIZE * sizeof(struct netsvctype_dscp_map);
	struct netsvctype_dscp_map netsvctype_dscp_map[DSCP_ARRAY_SIZE] = {};
	struct dcsp_msc_map dcsp_msc_map[DSCP_ARRAY_SIZE];
	size_t count;

	if (req->oldptr == USER_ADDR_NULL) {
		req->oldidx = len;
	} else if (req->oldlen > 0) {
		uint8_t i;

		for (i = 0; i < DSCP_ARRAY_SIZE; i++) {
			netsvctype_dscp_map[i].dscp = i;
			netsvctype_dscp_map[i].netsvctype =
			    so_svc2tc(wifi_dscp_to_msc_array[i]);
		}
		error = SYSCTL_OUT(req, netsvctype_dscp_map,
		    MIN(len, req->oldlen));
		if (error != 0) {
			goto done;
		}
	}

	if (req->newptr == USER_ADDR_NULL) {
		goto done;
	}

	error = proc_suser(current_proc());
	if (error != 0) {
		goto done;
	}

	/*
	 * Check input length
	 */
	if (req->newlen > len) {
		error = EINVAL;
		goto done;
	}
	/*
	 * Cap the number of entries to copy from input buffer
	 */
	if (len > req->newlen) {
		len = req->newlen;
	}
	error = SYSCTL_IN(req, netsvctype_dscp_map, len);
	if (error != 0) {
		goto done;
	}
	count = len / sizeof(struct netsvctype_dscp_map);
	bzero(dcsp_msc_map, sizeof(dcsp_msc_map));
	error = dscp_msc_map_from_netsvctype_dscp_map(netsvctype_dscp_map, count,
	    dcsp_msc_map);
	if (error != 0) {
		goto done;
	}
	set_dscp_to_wifi_ac_map(dcsp_msc_map, 0);
done:
	return error;
}

int
sysctl_reset_dscp_to_wifi_ac_map SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg1, arg2)
	int error = 0;
	int val = 0;

	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr) {
		return error;
	}
	if (req->newptr == USER_ADDR_NULL) {
		return 0;
	}
	error = proc_suser(current_proc());
	if (error != 0) {
		return error;
	}

	set_dscp_to_wifi_ac_map(default_dscp_to_wifi_ac_map, 1);

	return 0;
}

/*
 * Returns whether a large upload or download transfer should be marked as
 * BK service type for network activity. This is a system level
 * hint/suggestion to classify application traffic based on statistics
 * collected from the current network attachment
 *
 * Returns 1 for BK and 0 for default
 */

int
net_qos_guideline(struct proc *p, struct net_qos_guideline_args *arg,
    int *retval)
{
#pragma unused(p)
#define RETURN_USE_BK   1
#define RETURN_USE_DEFAULT      0
	struct net_qos_param qos_arg;
	struct ifnet *ipv4_primary, *ipv6_primary;
	int err = 0;

	if (arg->param == USER_ADDR_NULL || retval == NULL ||
	    arg->param_len != sizeof(qos_arg)) {
		return EINVAL;
	}
	err = copyin(arg->param, (caddr_t) &qos_arg, sizeof(qos_arg));
	if (err != 0) {
		return err;
	}

	*retval = RETURN_USE_DEFAULT;
	ipv4_primary = ifindex2ifnet[get_primary_ifscope(AF_INET)];
	ipv6_primary = ifindex2ifnet[get_primary_ifscope(AF_INET6)];

	/*
	 * If either of the interfaces is in Low Internet mode, enable
	 * background delay based algorithms on this transfer
	 */
	if (qos_arg.nq_uplink) {
		if ((ipv4_primary != NULL &&
		    (ipv4_primary->if_xflags & IFXF_LOW_INTERNET_UL)) ||
		    (ipv6_primary != NULL &&
		    (ipv6_primary->if_xflags & IFXF_LOW_INTERNET_UL))) {
			*retval = RETURN_USE_BK;
			return 0;
		}
	} else {
		if ((ipv4_primary != NULL &&
		    (ipv4_primary->if_xflags & IFXF_LOW_INTERNET_DL)) ||
		    (ipv6_primary != NULL &&
		    (ipv6_primary->if_xflags & IFXF_LOW_INTERNET_DL))) {
			*retval = RETURN_USE_BK;
			return 0;
		}
	}

	/*
	 * Some times IPv4 and IPv6 primary interfaces can be different.
	 * In this case, if either of them is non-cellular, we should mark
	 * the transfer as BK as it can potentially get used based on
	 * the host name resolution
	 */
	if (ipv4_primary != NULL && IFNET_IS_EXPENSIVE(ipv4_primary) &&
	    ipv6_primary != NULL && IFNET_IS_EXPENSIVE(ipv6_primary)) {
		if (qos_arg.nq_use_expensive) {
			return 0;
		} else {
			*retval = RETURN_USE_BK;
			return 0;
		}
	}
	if (ipv4_primary != NULL && IFNET_IS_CONSTRAINED(ipv4_primary) &&
	    ipv6_primary != NULL && IFNET_IS_CONSTRAINED(ipv6_primary)) {
		if (qos_arg.nq_use_constrained) {
			return 0;
		} else {
			*retval = RETURN_USE_BK;
			return 0;
		}
	}
	if (qos_arg.nq_transfer_size >= 5 * 1024 * 1024) {
		*retval = RETURN_USE_BK;
		return 0;
	}


#undef  RETURN_USE_BK
#undef  RETURN_USE_DEFAULT
	return 0;
}

#if (DEBUG || DEVELOPMENT)
/*
 * Customizable QoS mapping table
 * By default it uses the mapping table for RFC 4594
 *
 * Notes:
 *   BK_SYS is the same as BK
 *   CTL cannot be changed and is always _DSCP_CS6
 */
SYSCTL_NODE(_net_qos, OID_AUTO, custom,
    CTLFLAG_RW | CTLFLAG_LOCKED, 0, "");

SYSCTL_NODE(_net_qos_custom, OID_AUTO, netsvctype_to_dscp,
    CTLFLAG_RW | CTLFLAG_LOCKED, 0, "");

static int sysctl_net_qos_custom_netsvctype_to_dscp SYSCTL_HANDLER_ARGS;
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, be,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_BE, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, bk,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_BK, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, sig,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_SIG, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, vi,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_VI, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, vo,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_VO, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, rv,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_RV, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, av,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_AV, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, oam,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_OAM, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");
SYSCTL_PROC(_net_qos_custom_netsvctype_to_dscp, OID_AUTO, rd,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, NET_SERVICE_TYPE_RD, sysctl_net_qos_custom_netsvctype_to_dscp, "I", "");

static int sysctl_net_qos_custom_reset SYSCTL_HANDLER_ARGS;
SYSCTL_PROC(_net_qos_custom, OID_AUTO, reset,
    CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED,
    0, 0, sysctl_net_qos_custom_reset, "I", "");

int
sysctl_net_qos_custom_netsvctype_to_dscp SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1)
	int error = 0;

	switch (arg2) {
	case NET_SERVICE_TYPE_BE:
	case NET_SERVICE_TYPE_BK:
	case NET_SERVICE_TYPE_SIG:
	case NET_SERVICE_TYPE_VI:
	case NET_SERVICE_TYPE_VO:
	case NET_SERVICE_TYPE_RV:
	case NET_SERVICE_TYPE_AV:
	case NET_SERVICE_TYPE_OAM:
	case NET_SERVICE_TYPE_RD:
		break;
	default:
		os_log(OS_LOG_DEFAULT, "%s: unexpected netsvctype %d",
		    __func__, arg2);
		return EINVAL;
	}

	int val = custom_net_qos_dscp_map.netsvctype_to_dscp[arg2];
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error != 0 || req->newptr == USER_ADDR_NULL) {
		return error;
	}
	if (req->newptr == USER_ADDR_NULL) {
		return 0;
	}
	error = proc_suser(current_proc());
	if (error != 0) {
		return error;
	}
	if (val < 0 || val > _MAX_DSCP) {
		os_log(OS_LOG_DEFAULT, "%s: unexpected DSCP %d",
		    __func__, val);
		return EINVAL;
	}

	struct netsvctype_dscp_map netsvctype_dscp_map[_NET_SERVICE_TYPE_COUNT] = {};

	for (int i = 0; i < _NET_SERVICE_TYPE_COUNT; i++) {
		netsvctype_dscp_map[i].netsvctype = i;
		netsvctype_dscp_map[i].dscp = custom_net_qos_dscp_map.netsvctype_to_dscp[i];
	}
	netsvctype_dscp_map[arg2].dscp = (uint8_t) val;

	error = set_netsvctype_dscp_map(&custom_net_qos_dscp_map,
	    netsvctype_dscp_map);

	return 0;
}

int
sysctl_net_qos_custom_reset SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int error = 0;
	int val = 0;

	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr) {
		return error;
	}
	if (req->newptr == USER_ADDR_NULL) {
		return 0;
	}
	error = proc_suser(current_proc());
	if (error != 0) {
		return error;
	}

	error = set_netsvctype_dscp_map(&custom_net_qos_dscp_map,
	    rfc4594_netsvctype_dscp_map);

	return error;
}

uint8_t
custom_sc_to_dscp(uint32_t svc_class)
{
	uint8_t dscp = _DSCP_DF;

	switch (svc_class) {
	case MBUF_SC_BK_SYS:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_BK_SYS];
		break;
	case MBUF_SC_BK:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_BK];
		break;

	case MBUF_SC_BE:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_BE];
		break;
	case MBUF_SC_RD:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_RD];
		break;
	case MBUF_SC_OAM:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_OAM];
		break;

	case MBUF_SC_AV:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_AV];
		break;
	case MBUF_SC_RV:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_RV];
		break;
	case MBUF_SC_VI:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_VI];
		break;
	case MBUF_SC_SIG:
		dscp = custom_net_qos_dscp_map.netsvctype_to_dscp[NET_SERVICE_TYPE_SIG];
		break;

	case MBUF_SC_VO:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_VO];
		break;
	case MBUF_SC_CTL:
		dscp = custom_net_qos_dscp_map.sotc_to_dscp[SOTCIX_CTL];
		break;
	default:
		break;
	}
	return dscp;
}
#endif /* (DEBUG || DEVELOPMENT) */