DragonFlyBSD Kernel Audit
sys/net/dummynet3/ip_dummynet3.c
← back
   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
/*
 * Copyright (c) 1998-2002 Luigi Rizzo, Universita` di Pisa
 * Portions Copyright (c) 2000 Akamba Corp.
 * All rights reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: src/sys/netinet/ip_dummynet.c,v 1.24.2.22 2003/05/13 09:31:06 maxim Exp $
 */

#include "opt_ipfw.h"
#include "opt_inet.h"
#ifndef INET
#error IPFIREWALL3 requires INET.
#endif /* INET */

/*
 * This module implements IP dummynet, a bandwidth limiter/delay emulator.
 * Description of the data structures used is in ip_dummynet.h
 * Here you mainly find the following blocks of code:
 *  + variable declarations;
 *  + heap management functions;
 *  + scheduler and dummynet functions;
 *  + configuration and initialization.
 *
 * Most important Changes:
 *
 * 011004: KLDable
 * 010124: Fixed WF2Q behaviour
 * 010122: Fixed spl protection.
 * 000601: WF2Q support
 * 000106: Large rewrite, use heaps to handle very many pipes.
 * 980513: Initial release
 */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/systimer.h>
#include <sys/thread2.h>

#include <net/ethernet.h>
#include <net/netmsg2.h>
#include <net/netisr2.h>
#include <net/route.h>

#include <net/if.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>

#include <net/dummynet3/ip_dummynet3.h>
#include <net/ipfw3/ip_fw.h>

void check_pipe(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args,
		struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len);
void check_queue(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args,
		struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len);

void
check_pipe(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args,
		struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len)
{
	(*args)->rule = *f;
	(*args)->cookie = cmd->arg1;
	*cmd_val = IP_FW_DUMMYNET;
	*cmd_ctl = IP_FW_CTL_DONE;
}

void
check_queue(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args,
		struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len)
{
	(*args)->rule = *f;
	(*args)->cookie = cmd->arg1;
	*cmd_val = IP_FW_DUMMYNET;
	*cmd_ctl = IP_FW_CTL_DONE;
}

#ifdef DUMMYNET_DEBUG
#define DPRINTF(fmt, ...)	kprintf(fmt, __VA_ARGS__)
#else
#define DPRINTF(fmt, ...)	((void)0)
#endif

#ifndef DN_CALLOUT_FREQ_MAX
#define DN_CALLOUT_FREQ_MAX	10000
#endif

/*
 * The maximum/minimum hash table size for queues.
 * These values must be a power of 2.
 */
#define DN_MIN_HASH_SIZE	4
#define DN_MAX_HASH_SIZE	65536

/*
 * Some macros are used to compare key values and handle wraparounds.
 * MAX64 returns the largest of two key values.
 */
#define DN_KEY_LT(a, b)		((int64_t)((a) - (b)) < 0)
#define DN_KEY_LEQ(a, b)	((int64_t)((a) - (b)) <= 0)
#define DN_KEY_GT(a, b)		((int64_t)((a) - (b)) > 0)
#define DN_KEY_GEQ(a, b)	((int64_t)((a) - (b)) >= 0)
#define MAX64(x, y)		((((int64_t)((y) - (x))) > 0) ? (y) : (x))

#define DN_NR_HASH_MAX		16
#define DN_NR_HASH_MASK		(DN_NR_HASH_MAX - 1)
#define DN_NR_HASH(nr)		\
	((((nr) >> 12) ^ ((nr) >> 8) ^ ((nr) >> 4) ^ (nr)) & DN_NR_HASH_MASK)

MALLOC_DEFINE(M_DUMMYNET, "dummynet", "dummynet heap");

extern int	ip_dn_cpu;

static dn_key	curr_time = 0;		/* current simulation time */
static int	dn_hash_size = 64;	/* default hash size */
static int	pipe_expire = 1;	/* expire queue if empty */
static int	dn_max_ratio = 16;	/* max queues/buckets ratio */

/*
 * Statistics on number of queue searches and search steps
 */
static int	searches;
static int	search_steps;

/*
 * RED parameters
 */
static int	red_lookup_depth = 256;	/* default lookup table depth */
static int	red_avg_pkt_size = 512;	/* default medium packet size */
static int	red_max_pkt_size = 1500;/* default max packet size */

/*
 * Three heaps contain queues and pipes that the scheduler handles:
 *
 *  + ready_heap	contains all dn_flow_queue related to fixed-rate pipes.
 *  + wfq_ready_heap	contains the pipes associated with WF2Q flows
 *  + extract_heap	contains pipes associated with delay lines.
 */
static struct dn_heap	ready_heap;
static struct dn_heap	extract_heap;
static struct dn_heap	wfq_ready_heap;

static struct dn_pipe_head	pipe_table[DN_NR_HASH_MAX];
static struct dn_flowset_head	flowset_table[DN_NR_HASH_MAX];

/*
 * Variables for dummynet systimer
 */
static struct netmsg_base dn_netmsg;
static struct systimer	dn_clock;
static int		dn_hz = 1000;

static int	sysctl_dn_hz(SYSCTL_HANDLER_ARGS);

SYSCTL_DECL(_net_inet_ip_dummynet);

SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, hash_size, CTLFLAG_RW,
		&dn_hash_size, 0, "Default hash table size");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, curr_time, CTLFLAG_RD,
		&curr_time, 0, "Current tick");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, expire, CTLFLAG_RW,
		&pipe_expire, 0, "Expire queue if empty");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, max_chain_len, CTLFLAG_RW,
		&dn_max_ratio, 0, "Max ratio between dynamic queues and buckets");

SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, ready_heap, CTLFLAG_RD,
		&ready_heap.size, 0, "Size of ready heap");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, extract_heap, CTLFLAG_RD,
		&extract_heap.size, 0, "Size of extract heap");

SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, searches, CTLFLAG_RD,
		&searches, 0, "Number of queue searches");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, search_steps, CTLFLAG_RD,
		&search_steps, 0, "Number of queue search steps");

SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_lookup_depth, CTLFLAG_RD,
		&red_lookup_depth, 0, "Depth of RED lookup table");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_avg_pkt_size, CTLFLAG_RD,
		&red_avg_pkt_size, 0, "RED Medium packet size");
SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_max_pkt_size, CTLFLAG_RD,
		&red_max_pkt_size, 0, "RED Max packet size");

SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, hz, CTLTYPE_INT | CTLFLAG_RW,
		0, 0, sysctl_dn_hz, "I", "Dummynet callout frequency");

static int	heap_init(struct dn_heap *, int);
static int	heap_insert(struct dn_heap *, dn_key, void *);
static void	heap_extract(struct dn_heap *, void *);

static void	transmit_event(struct dn_pipe *);
static void	ready_event(struct dn_flow_queue *);
static void	ready_event_wfq(struct dn_pipe *);

static int	config_pipe(struct dn_ioc_pipe *);
static void	dummynet_flush(void);

static void	dummynet_clock(systimer_t, int, struct intrframe *);
static void	dummynet(netmsg_t);

static struct dn_pipe *dn_find_pipe(int);
static struct dn_flow_set *dn_locate_flowset(int, int);

typedef void	(*dn_pipe_iter_t)(struct dn_pipe *, void *);
static void	dn_iterate_pipe(dn_pipe_iter_t, void *);

typedef void	(*dn_flowset_iter_t)(struct dn_flow_set *, void *);
static void	dn_iterate_flowset(dn_flowset_iter_t, void *);

static ip_dn_io_t	dummynet_io;
static ip_dn_ctl_t	dummynet_ctl;

/*
 * Heap management functions.
 *
 * In the heap, first node is element 0. Children of i are 2i+1 and 2i+2.
 * Some macros help finding parent/children so we can optimize them.
 *
 * heap_init() is called to expand the heap when needed.
 * Increment size in blocks of 16 entries.
 * XXX failure to allocate a new element is a pretty bad failure
 * as we basically stall a whole queue forever!!
 * Returns 1 on error, 0 on success
 */
#define HEAP_FATHER(x)		(((x) - 1) / 2)
#define HEAP_LEFT(x)		(2*(x) + 1)
#define HEAP_IS_LEFT(x)		((x) & 1)
#define HEAP_RIGHT(x)		(2*(x) + 2)
#define HEAP_SWAP(a, b, buffer)	{ buffer = a; a = b; b = buffer; }
#define HEAP_INCREMENT		15

static int
heap_init(struct dn_heap *h, int new_size)
{
	struct dn_heap_entry *p;

	if (h->size >= new_size) {
		kprintf("%s, Bogus call, have %d want %d\n", __func__,
				h->size, new_size);
		return 0;
	}

	new_size = (new_size + HEAP_INCREMENT) & ~HEAP_INCREMENT;
	p = kmalloc(new_size * sizeof(*p), M_DUMMYNET, M_WAITOK | M_ZERO);
	if (h->size > 0) {
		bcopy(h->p, p, h->size * sizeof(*p));
		kfree(h->p, M_DUMMYNET);
	}
	h->p = p;
	h->size = new_size;
	return 0;
}

/*
 * Insert element in heap. Normally, p != NULL, we insert p in
 * a new position and bubble up.  If p == NULL, then the element is
 * already in place, and key is the position where to start the
 * bubble-up.
 * Returns 1 on failure (cannot allocate new heap entry)
 *
 * If offset > 0 the position (index, int) of the element in the heap is
 * also stored in the element itself at the given offset in bytes.
 */
#define SET_OFFSET(heap, node) \
	if (heap->offset > 0) \
	*((int *)((char *)(heap->p[node].object) + heap->offset)) = node;

/*
 * RESET_OFFSET is used for sanity checks. It sets offset to an invalid value.
 */
#define RESET_OFFSET(heap, node) \
	if (heap->offset > 0) \
	*((int *)((char *)(heap->p[node].object) + heap->offset)) = -1;

static int
heap_insert(struct dn_heap *h, dn_key key1, void *p)
{
	int son;

	if (p == NULL) {	/* Data already there, set starting point */
		son = key1;
	} else {		/* Insert new element at the end, possibly resize */
		son = h->elements;
		if (son == h->size) { /* Need resize... */
			if (heap_init(h, h->elements + 1))
				return 1; /* Failure... */
		}
		h->p[son].object = p;
		h->p[son].key = key1;
		h->elements++;
	}

	while (son > 0) {	/* Bubble up */
		int father = HEAP_FATHER(son);
		struct dn_heap_entry tmp;

		if (DN_KEY_LT(h->p[father].key, h->p[son].key))
			break; /* Found right position */

		/* 'son' smaller than 'father', swap and repeat */
		HEAP_SWAP(h->p[son], h->p[father], tmp);
		SET_OFFSET(h, son);
		son = father;
	}
	SET_OFFSET(h, son);
	return 0;
}

/*
 * Remove top element from heap, or obj if obj != NULL
 */
static void
heap_extract(struct dn_heap *h, void *obj)
{
	int child, father, max = h->elements - 1;

	if (max < 0) {
		kprintf("warning, extract from empty heap 0x%p\n", h);
		return;
	}

	father = 0; /* Default: move up smallest child */
	if (obj != NULL) { /* Extract specific element, index is at offset */
		if (h->offset <= 0)
			panic("%s from middle not supported on this heap!!!", __func__);

		father = *((int *)((char *)obj + h->offset));
		if (father < 0 || father >= h->elements) {
			panic("%s father %d out of bound 0..%d", __func__,
					father, h->elements);
		}
	}
	RESET_OFFSET(h, father);

	child = HEAP_LEFT(father);		/* Left child */
	while (child <= max) {		/* Valid entry */
		if (child != max && DN_KEY_LT(h->p[child + 1].key, h->p[child].key))
			child = child + 1;		/* Take right child, otherwise left */
		h->p[father] = h->p[child];
		SET_OFFSET(h, father);
		father = child;
		child = HEAP_LEFT(child);	/* Left child for next loop */
	}
	h->elements--;
	if (father != max) {
		/*
		 * Fill hole with last entry and bubble up, reusing the insert code
		 */
		h->p[father] = h->p[max];
		heap_insert(h, father, NULL);	/* This one cannot fail */
	}
}

/*
 * heapify() will reorganize data inside an array to maintain the
 * heap property.  It is needed when we delete a bunch of entries.
 */
static void
heapify(struct dn_heap *h)
{
	int i;

	for (i = 0; i < h->elements; i++)
		heap_insert(h, i , NULL);
}

/*
 * Cleanup the heap and free data structure
 */
static void
heap_free(struct dn_heap *h)
{
	if (h->size > 0)
		kfree(h->p, M_DUMMYNET);
	bzero(h, sizeof(*h));
}

/*
 * --- End of heap management functions ---
 */

/*
 * Scheduler functions:
 *
 * transmit_event() is called when the delay-line needs to enter
 * the scheduler, either because of existing pkts getting ready,
 * or new packets entering the queue.  The event handled is the delivery
 * time of the packet.
 *
 * ready_event() does something similar with fixed-rate queues, and the
 * event handled is the finish time of the head pkt.
 *
 * ready_event_wfq() does something similar with WF2Q queues, and the
 * event handled is the start time of the head pkt.
 *
 * In all cases, we make sure that the data structures are consistent
 * before passing pkts out, because this might trigger recursive
 * invocations of the procedures.
 */
static void
transmit_event(struct dn_pipe *pipe)
{
	struct dn_pkt *pkt;

	while ((pkt = TAILQ_FIRST(&pipe->p_queue)) &&
			DN_KEY_LEQ(pkt->output_time, curr_time)) {
		TAILQ_REMOVE(&pipe->p_queue, pkt, dn_next);
		ip_dn_packet_redispatch(pkt);
	}

	/*
	 * If there are leftover packets, put into the heap for next event
	 */
	if ((pkt = TAILQ_FIRST(&pipe->p_queue)) != NULL) {
		/*
		 * XXX should check errors on heap_insert, by draining the
		 * whole pipe and hoping in the future we are more successful
		 */
		heap_insert(&extract_heap, pkt->output_time, pipe);
	}
}

/*
 * The following macro computes how many ticks we have to wait
 * before being able to transmit a packet. The credit is taken from
 * either a pipe (WF2Q) or a flow_queue (per-flow queueing)
 */
#define SET_TICKS(pkt, q, p)	\
	(pkt->dn_m->m_pkthdr.len*8*dn_hz - (q)->numbytes + p->bandwidth - 1 ) / \
		p->bandwidth;

/*
 * Extract pkt from queue, compute output time (could be now)
 * and put into delay line (p_queue)
 */
static void
move_pkt(struct dn_pkt *pkt, struct dn_flow_queue *q,
	 struct dn_pipe *p, int len)
{
	TAILQ_REMOVE(&q->queue, pkt, dn_next);
	q->len--;
	q->len_bytes -= len;

	pkt->output_time = curr_time + p->delay;

	TAILQ_INSERT_TAIL(&p->p_queue, pkt, dn_next);
}

/*
 * ready_event() is invoked every time the queue must enter the
 * scheduler, either because the first packet arrives, or because
 * a previously scheduled event fired.
 * On invokation, drain as many pkts as possible (could be 0) and then
 * if there are leftover packets reinsert the pkt in the scheduler.
 */
static void
ready_event(struct dn_flow_queue *q)
{
	struct dn_pkt *pkt;
	struct dn_pipe *p = q->fs->pipe;
	int p_was_empty;

	if (p == NULL) {
		kprintf("ready_event- pipe is gone\n");
		return;
	}
	p_was_empty = TAILQ_EMPTY(&p->p_queue);

	/*
	 * Schedule fixed-rate queues linked to this pipe:
	 * Account for the bw accumulated since last scheduling, then
	 * drain as many pkts as allowed by q->numbytes and move to
	 * the delay line (in p) computing output time.
	 * bandwidth==0 (no limit) means we can drain the whole queue,
	 * setting len_scaled = 0 does the job.
	 */
	q->numbytes += (curr_time - q->sched_time) * p->bandwidth;
	while ((pkt = TAILQ_FIRST(&q->queue)) != NULL) {
		int len = pkt->dn_m->m_pkthdr.len;
		int len_scaled = p->bandwidth ? len*8*dn_hz : 0;

		if (len_scaled > q->numbytes)
			break;
		q->numbytes -= len_scaled;
		move_pkt(pkt, q, p, len);
	}

	/*
	 * If we have more packets queued, schedule next ready event
	 * (can only occur when bandwidth != 0, otherwise we would have
	 * flushed the whole queue in the previous loop).
	 * To this purpose we record the current time and compute how many
	 * ticks to go for the finish time of the packet.
	 */
	if ((pkt = TAILQ_FIRST(&q->queue)) != NULL) {
		/* This implies bandwidth != 0 */
		dn_key t = SET_TICKS(pkt, q, p); /* ticks i have to wait */

		q->sched_time = curr_time;

		/*
		 * XXX should check errors on heap_insert, and drain the whole
		 * queue on error hoping next time we are luckier.
		 */
		heap_insert(&ready_heap, curr_time + t, q);
	} else {	/* RED needs to know when the queue becomes empty */
		q->q_time = curr_time;
		q->numbytes = 0;
	}

	/*
	 * If the delay line was empty call transmit_event(p) now.
	 * Otherwise, the scheduler will take care of it.
	 */
	if (p_was_empty)
		transmit_event(p);
}

/*
 * Called when we can transmit packets on WF2Q queues.  Take pkts out of
 * the queues at their start time, and enqueue into the delay line.
 * Packets are drained until p->numbytes < 0.  As long as
 * len_scaled >= p->numbytes, the packet goes into the delay line
 * with a deadline p->delay.  For the last packet, if p->numbytes < 0,
 * there is an additional delay.
 */
static void
ready_event_wfq(struct dn_pipe *p)
{
	int p_was_empty = TAILQ_EMPTY(&p->p_queue);
	struct dn_heap *sch = &p->scheduler_heap;
	struct dn_heap *neh = &p->not_eligible_heap;

	p->numbytes += (curr_time - p->sched_time) * p->bandwidth;

	/*
	 * While we have backlogged traffic AND credit, we need to do
	 * something on the queue.
	 */
	while (p->numbytes >= 0 && (sch->elements > 0 || neh->elements > 0)) {
		if (sch->elements > 0) { /* Have some eligible pkts to send out */
			struct dn_flow_queue *q = sch->p[0].object;
			struct dn_pkt *pkt = TAILQ_FIRST(&q->queue);
			struct dn_flow_set *fs = q->fs;
			uint64_t len = pkt->dn_m->m_pkthdr.len;
			int len_scaled = p->bandwidth ? len*8*dn_hz : 0;

			heap_extract(sch, NULL);	/* Remove queue from heap */
			p->numbytes -= len_scaled;
			move_pkt(pkt, q, p, len);

			p->V += (len << MY_M) / p->sum;	/* Update V */
			q->S = q->F;			/* Update start time */

			if (q->len == 0) {	/* Flow not backlogged any more */
				fs->backlogged--;
				heap_insert(&p->idle_heap, q->F, q);
			} else {		/* Still backlogged */
				/*
				 * Update F and position in backlogged queue, then
				 * put flow in not_eligible_heap (we will fix this later).
				 */
				len = TAILQ_FIRST(&q->queue)->dn_m->m_pkthdr.len;
				q->F += (len << MY_M) / (uint64_t)fs->weight;
				if (DN_KEY_LEQ(q->S, p->V))
					heap_insert(neh, q->S, q);
				else
					heap_insert(sch, q->F, q);
			}
		}

		/*
		 * Now compute V = max(V, min(S_i)).  Remember that all elements in
		 * sch have by definition S_i <= V so if sch is not empty, V is surely
		 * the max and we must not update it.  Conversely, if sch is empty
		 * we only need to look at neh.
		 */
		if (sch->elements == 0 && neh->elements > 0)
			p->V = MAX64(p->V, neh->p[0].key);

		/*
		 * Move from neh to sch any packets that have become eligible
		 */
		while (neh->elements > 0 && DN_KEY_LEQ(neh->p[0].key, p->V)) {
			struct dn_flow_queue *q = neh->p[0].object;

			heap_extract(neh, NULL);
			heap_insert(sch, q->F, q);
		}
	}

	if (sch->elements == 0 && neh->elements == 0 && p->numbytes >= 0 &&
			p->idle_heap.elements > 0) {
		/*
		 * No traffic and no events scheduled.  We can get rid of idle-heap.
		 */
		int i;

		for (i = 0; i < p->idle_heap.elements; i++) {
			struct dn_flow_queue *q = p->idle_heap.p[i].object;

			q->F = 0;
			q->S = q->F + 1;
		}
		p->sum = 0;
		p->V = 0;
		p->idle_heap.elements = 0;
	}

	/*
	 * If we are getting clocks from dummynet and if we are under credit,
	 * schedule the next ready event.
	 * Also fix the delivery time of the last packet.
	 */
	if (p->numbytes < 0) { /* This implies bandwidth>0 */
		dn_key t = 0; /* Number of ticks i have to wait */

		if (p->bandwidth > 0)
			t = (p->bandwidth - 1 - p->numbytes) / p->bandwidth;
		TAILQ_LAST(&p->p_queue, dn_pkt_queue)->output_time += t;
		p->sched_time = curr_time;

		/*
		 * XXX should check errors on heap_insert, and drain the whole
		 * queue on error hoping next time we are luckier.
		 */
		heap_insert(&wfq_ready_heap, curr_time + t, p);
	}

	/*
	 * If the delay line was empty call transmit_event(p) now.
	 * Otherwise, the scheduler will take care of it.
	 */
	if (p_was_empty)
		transmit_event(p);
}

static void
dn_expire_pipe_cb(struct dn_pipe *pipe, void *dummy __unused)
{
	if (pipe->idle_heap.elements > 0 &&
			DN_KEY_LT(pipe->idle_heap.p[0].key, pipe->V)) {
		struct dn_flow_queue *q = pipe->idle_heap.p[0].object;

		heap_extract(&pipe->idle_heap, NULL);
		q->S = q->F + 1; /* Mark timestamp as invalid */
		pipe->sum -= q->fs->weight;
	}
}

/*
 * This is called once per tick, or dn_hz times per second.  It is used to
 * increment the current tick counter and schedule expired events.
 */
static void
dummynet(netmsg_t msg)
{
	void *p;
	struct dn_heap *h;
	struct dn_heap *heaps[3];
	int i;

	heaps[0] = &ready_heap;		/* Fixed-rate queues */
	heaps[1] = &wfq_ready_heap;		/* WF2Q queues */
	heaps[2] = &extract_heap;		/* Delay line */

	/* Reply ASAP */
	crit_enter();
	lwkt_replymsg(&msg->lmsg, 0);
	crit_exit();

	curr_time++;
	for (i = 0; i < 3; i++) {
		h = heaps[i];
		while (h->elements > 0 && DN_KEY_LEQ(h->p[0].key, curr_time)) {
			if (h->p[0].key > curr_time) {
				kprintf("-- dummynet: warning, heap %d is %d ticks late\n",
						i, (int)(curr_time - h->p[0].key));
			}

			p = h->p[0].object;		/* Store a copy before heap_extract */
			heap_extract(h, NULL);	/* Need to extract before processing */

			if (i == 0)
				ready_event(p);
			else if (i == 1)
				ready_event_wfq(p);
			else
				transmit_event(p);
		}
	}

	/* Sweep pipes trying to expire idle flow_queues */
	dn_iterate_pipe(dn_expire_pipe_cb, NULL);
}

/*
 * Unconditionally expire empty queues in case of shortage.
 * Returns the number of queues freed.
 */
static int
expire_queues(struct dn_flow_set *fs)
{
	int i, initial_elements = fs->rq_elements;

	if (fs->last_expired == time_uptime)
		return 0;

	fs->last_expired = time_uptime;

	for (i = 0; i <= fs->rq_size; i++) { /* Last one is overflow */
		struct dn_flow_queue *q, *qn;

		LIST_FOREACH_MUTABLE(q, &fs->rq[i], q_link, qn) {
			if (!TAILQ_EMPTY(&q->queue) || q->S != q->F + 1)
				continue;

			/*
			 * Entry is idle, expire it
			 */
			LIST_REMOVE(q, q_link);
			kfree(q, M_DUMMYNET);

			KASSERT(fs->rq_elements > 0,
					("invalid rq_elements %d", fs->rq_elements));
			fs->rq_elements--;
		}
	}
	return initial_elements - fs->rq_elements;
}

/*
 * If room, create a new queue and put at head of slot i;
 * otherwise, create or use the default queue.
 */
static struct dn_flow_queue *
create_queue(struct dn_flow_set *fs, int i)
{
	struct dn_flow_queue *q;

	if (fs->rq_elements > fs->rq_size * dn_max_ratio &&
			expire_queues(fs) == 0) {
		/*
		 * No way to get room, use or create overflow queue.
		 */
		i = fs->rq_size;
		if (!LIST_EMPTY(&fs->rq[i]))
			return LIST_FIRST(&fs->rq[i]);
	}

	q = kmalloc(sizeof(*q), M_DUMMYNET, M_INTWAIT | M_NULLOK | M_ZERO);
	if (q == NULL)
		return NULL;

	q->fs = fs;
	q->hash_slot = i;
	q->S = q->F + 1;   /* hack - mark timestamp as invalid */
	TAILQ_INIT(&q->queue);

	LIST_INSERT_HEAD(&fs->rq[i], q, q_link);
	fs->rq_elements++;

	return q;
}

/*
 * Given a flow_set and a pkt in last_pkt, find a matching queue
 * after appropriate masking. The queue is moved to front
 * so that further searches take less time.
 */
static struct dn_flow_queue *
find_queue(struct dn_flow_set *fs, struct dn_flow_id *id)
{
	struct dn_flow_queue *q;
	int i = 0;

	if (!(fs->flags_fs & DN_HAVE_FLOW_MASK)) {
		q = LIST_FIRST(&fs->rq[0]);
	} else {
		struct dn_flow_queue *qn;

		/* First, do the masking */
		id->fid_dst_ip &= fs->flow_mask.fid_dst_ip;
		id->fid_src_ip &= fs->flow_mask.fid_src_ip;
		id->fid_dst_port &= fs->flow_mask.fid_dst_port;
		id->fid_src_port &= fs->flow_mask.fid_src_port;
		id->fid_proto &= fs->flow_mask.fid_proto;
		id->fid_flags = 0; /* we don't care about this one */

		/* Then, hash function */
		i = ((id->fid_dst_ip) & 0xffff) ^
			((id->fid_dst_ip >> 15) & 0xffff) ^
			((id->fid_src_ip << 1) & 0xffff) ^
			((id->fid_src_ip >> 16 ) & 0xffff) ^
			(id->fid_dst_port << 1) ^ (id->fid_src_port) ^
			(id->fid_proto);
		i = i % fs->rq_size;

		/*
		 * Finally, scan the current list for a match and
		 * expire idle flow queues
		 */
		searches++;
		LIST_FOREACH_MUTABLE(q, &fs->rq[i], q_link, qn) {
			search_steps++;
			if (id->fid_dst_ip == q->id.fid_dst_ip &&
					id->fid_src_ip == q->id.fid_src_ip &&
					id->fid_dst_port == q->id.fid_dst_port &&
					id->fid_src_port == q->id.fid_src_port &&
					id->fid_proto == q->id.fid_proto &&
					id->fid_flags == q->id.fid_flags) {
				break; /* Found */
			} else if (pipe_expire && TAILQ_EMPTY(&q->queue) &&
					q->S == q->F + 1) {
				/*
				 * Entry is idle and not in any heap, expire it
				 */
				LIST_REMOVE(q, q_link);
				kfree(q, M_DUMMYNET);

				KASSERT(fs->rq_elements > 0,
						("invalid rq_elements %d", fs->rq_elements));
				fs->rq_elements--;
			}
		}
		if (q && LIST_FIRST(&fs->rq[i]) != q) { /* Found and not in front */
			LIST_REMOVE(q, q_link);
			LIST_INSERT_HEAD(&fs->rq[i], q, q_link);
		}
	}
	if (q == NULL) {	/* No match, need to allocate a new entry */
		q = create_queue(fs, i);
		if (q != NULL)
			q->id = *id;
	}
	return q;
}

static int
red_drops(struct dn_flow_set *fs, struct dn_flow_queue *q, int len)
{
	/*
	 * RED algorithm
	 *
	 * RED calculates the average queue size (avg) using a low-pass filter
	 * with an exponential weighted (w_q) moving average:
	 * 	avg  <-  (1-w_q) * avg + w_q * q_size
	 * where q_size is the queue length (measured in bytes or * packets).
	 *
	 * If q_size == 0, we compute the idle time for the link, and set
	 *	avg = (1 - w_q)^(idle/s)
	 * where s is the time needed for transmitting a medium-sized packet.
	 *
	 * Now, if avg < min_th the packet is enqueued.
	 * If avg > max_th the packet is dropped. Otherwise, the packet is
	 * dropped with probability P function of avg.
	 */

	int64_t p_b = 0;
	u_int q_size = (fs->flags_fs & DN_QSIZE_IS_BYTES) ? q->len_bytes : q->len;

	IPFW3_DEBUG("\n%d q: %2u ", (int)curr_time, q_size);

	/* Average queue size estimation */
	if (q_size != 0) {
		/*
		 * Queue is not empty, avg <- avg + (q_size - avg) * w_q
		 */
		int diff = SCALE(q_size) - q->avg;
		int64_t v = SCALE_MUL((int64_t)diff, (int64_t)fs->w_q);

		q->avg += (int)v;
	} else {
		/*
		 * Queue is empty, find for how long the queue has been
		 * empty and use a lookup table for computing
		 * (1 - * w_q)^(idle_time/s) where s is the time to send a
		 * (small) packet.
		 * XXX check wraps...
		 */
		if (q->avg) {
			u_int t = (curr_time - q->q_time) / fs->lookup_step;

			q->avg = (t < fs->lookup_depth) ?
				SCALE_MUL(q->avg, fs->w_q_lookup[t]) : 0;
		}
	}
	IPFW3_DEBUG("avg: %u ", SCALE_VAL(q->avg));

	/* Should i drop? */

	if (q->avg < fs->min_th) {
		/* Accept packet */
		q->count = -1;
		return 0;
	}

	if (q->avg >= fs->max_th) { /* Average queue >=  Max threshold */
		if (fs->flags_fs & DN_IS_GENTLE_RED) {
			/*
			 * According to Gentle-RED, if avg is greater than max_th the
			 * packet is dropped with a probability
			 *	p_b = c_3 * avg - c_4
			 * where c_3 = (1 - max_p) / max_th, and c_4 = 1 - 2 * max_p
			 */
			p_b = SCALE_MUL((int64_t)fs->c_3, (int64_t)q->avg) - fs->c_4;
		} else {
			q->count = -1;
			kprintf("- drop\n");
			return 1;
		}
	} else if (q->avg > fs->min_th) {
		/*
		 * We compute p_b using the linear dropping function p_b = c_1 *
		 * avg - c_2, where c_1 = max_p / (max_th - min_th), and c_2 =
		 * max_p * min_th / (max_th - min_th)
		 */
		p_b = SCALE_MUL((int64_t)fs->c_1, (int64_t)q->avg) - fs->c_2;
	}
	if (fs->flags_fs & DN_QSIZE_IS_BYTES)
		p_b = (p_b * len) / fs->max_pkt_size;

	if (++q->count == 0) {
		q->random = krandom() & 0xffff;
	} else {
		/*
		 * q->count counts packets arrived since last drop, so a greater
		 * value of q->count means a greater packet drop probability.
		 */
		if (SCALE_MUL(p_b, SCALE((int64_t)q->count)) > q->random) {
			q->count = 0;
			IPFW3_DEBUG("%s", "- red drop");
			/* After a drop we calculate a new random value */
			q->random = krandom() & 0xffff;
			return 1;	/* Drop */
		}
	}
	/* End of RED algorithm */
	return 0; /* Accept */
}

static void
dn_iterate_pipe(dn_pipe_iter_t func, void *arg)
{
	int i;

	for (i = 0; i < DN_NR_HASH_MAX; ++i) {
		struct dn_pipe_head *pipe_hdr = &pipe_table[i];
		struct dn_pipe *pipe, *pipe_next;

		LIST_FOREACH_MUTABLE(pipe, pipe_hdr, p_link, pipe_next)
			func(pipe, arg);
	}
}

static void
dn_iterate_flowset(dn_flowset_iter_t func, void *arg)
{
	int i;

	for (i = 0; i < DN_NR_HASH_MAX; ++i) {
		struct dn_flowset_head *fs_hdr = &flowset_table[i];
		struct dn_flow_set *fs, *fs_next;

		LIST_FOREACH_MUTABLE(fs, fs_hdr, fs_link, fs_next)
			func(fs, arg);
	}
}

static struct dn_pipe *
dn_find_pipe(int pipe_nr)
{
	struct dn_pipe_head *pipe_hdr;
	struct dn_pipe *p;

	pipe_hdr = &pipe_table[DN_NR_HASH(pipe_nr)];
	LIST_FOREACH(p, pipe_hdr, p_link) {
		if (p->pipe_nr == pipe_nr)
			break;
	}
	return p;
}

static struct dn_flow_set *
dn_find_flowset(int fs_nr)
{
	struct dn_flowset_head *fs_hdr;
	struct dn_flow_set *fs;

	fs_hdr = &flowset_table[DN_NR_HASH(fs_nr)];
	LIST_FOREACH(fs, fs_hdr, fs_link) {
		if (fs->fs_nr == fs_nr)
			break;
	}
	return fs;
}

static struct dn_flow_set *
dn_locate_flowset(int pipe_nr, int is_pipe)
{
	struct dn_flow_set *fs = NULL;

	if (!is_pipe) {
		fs = dn_find_flowset(pipe_nr);
	} else {
		struct dn_pipe *p;

		p = dn_find_pipe(pipe_nr);
		if (p != NULL)
			fs = &p->fs;
	}
	return fs;
}

/*
 * Dummynet hook for packets.  Below 'pipe' is a pipe or a queue
 * depending on whether WF2Q or fixed bw is used.
 *
 * pipe_nr	pipe or queue the packet is destined for.
 * dir		where shall we send the packet after dummynet.
 * m		the mbuf with the packet
 * fwa->oif	the 'ifp' parameter from the caller.
 *		NULL in ip_input, destination interface in ip_output
 * fwa->ro	route parameter (only used in ip_output, NULL otherwise)
 * fwa->dst	destination address, only used by ip_output
 * fwa->rule	matching rule, in case of multiple passes
 * fwa->flags	flags from the caller, only used in ip_output
 */
static int
dummynet_io(struct mbuf *m)
{
	struct dn_pkt *pkt;
	struct m_tag *tag;
	struct dn_flow_set *fs;
	struct dn_pipe *pipe;
	uint64_t len = m->m_pkthdr.len;
	struct dn_flow_queue *q = NULL;
	int is_pipe, pipe_nr;

	tag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
	pkt = m_tag_data(tag);

	is_pipe = pkt->dn_flags & DN_FLAGS_IS_PIPE;
	pipe_nr = pkt->pipe_nr;

	/*
	 * This is a dummynet rule, so we expect a O_PIPE or O_QUEUE rule
	 */
	fs = dn_locate_flowset(pipe_nr, is_pipe);
	if (fs == NULL)
		goto dropit;	/* This queue/pipe does not exist! */

	pipe = fs->pipe;
	if (pipe == NULL) { /* Must be a queue, try find a matching pipe */
		pipe = dn_find_pipe(fs->parent_nr);
		if (pipe != NULL) {
			fs->pipe = pipe;
		} else {
			kprintf("No pipe %d for queue %d, drop pkt\n",
					fs->parent_nr, fs->fs_nr);
			goto dropit;
		}
	}

	q = find_queue(fs, &pkt->id);
	if (q == NULL)
		goto dropit;	/* Cannot allocate queue */

	/*
	 * Update statistics, then check reasons to drop pkt
	 */
	q->tot_bytes += len;
	q->tot_pkts++;

	if (fs->plr && krandom() < fs->plr)
		goto dropit;	/* Random pkt drop */

	if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
		if (q->len_bytes > fs->qsize)
			goto dropit;	/* Queue size overflow */
	} else {
		if (q->len >= fs->qsize)
			goto dropit;	/* Queue count overflow */
	}

	if ((fs->flags_fs & DN_IS_RED) && red_drops(fs, q, len))
		goto dropit;

	TAILQ_INSERT_TAIL(&q->queue, pkt, dn_next);
	q->len++;
	q->len_bytes += len;

	if (TAILQ_FIRST(&q->queue) != pkt)	/* Flow was not idle, we are done */
		goto done;

	/*
	 * If we reach this point the flow was previously idle, so we need
	 * to schedule it.  This involves different actions for fixed-rate
	 * or WF2Q queues.
	 */
	if (is_pipe) {
		/*
		 * Fixed-rate queue: just insert into the ready_heap.
		 */
		dn_key t = 0;

		if (pipe->bandwidth)
			t = SET_TICKS(pkt, q, pipe);

		q->sched_time = curr_time;
		if (t == 0)	/* Must process it now */
			ready_event(q);
		else
			heap_insert(&ready_heap, curr_time + t, q);
	} else {
		/*
		 * WF2Q:
		 * First, compute start time S: if the flow was idle (S=F+1)
		 * set S to the virtual time V for the controlling pipe, and update
		 * the sum of weights for the pipe; otherwise, remove flow from
		 * idle_heap and set S to max(F, V).
		 * Second, compute finish time F = S + len/weight.
		 * Third, if pipe was idle, update V = max(S, V).
		 * Fourth, count one more backlogged flow.
		 */
		if (DN_KEY_GT(q->S, q->F)) { /* Means timestamps are invalid */
			q->S = pipe->V;
			pipe->sum += fs->weight; /* Add weight of new queue */
		} else {
			heap_extract(&pipe->idle_heap, q);
			q->S = MAX64(q->F, pipe->V);
		}
		q->F = q->S + (len << MY_M) / (uint64_t)fs->weight;

		if (pipe->not_eligible_heap.elements == 0 &&
				pipe->scheduler_heap.elements == 0)
			pipe->V = MAX64(q->S, pipe->V);

		fs->backlogged++;

		/*
		 * Look at eligibility.  A flow is not eligibile if S>V (when
		 * this happens, it means that there is some other flow already
		 * scheduled for the same pipe, so the scheduler_heap cannot be
		 * empty).  If the flow is not eligible we just store it in the
		 * not_eligible_heap.  Otherwise, we store in the scheduler_heap
		 * and possibly invoke ready_event_wfq() right now if there is
		 * leftover credit.
		 * Note that for all flows in scheduler_heap (SCH), S_i <= V,
		 * and for all flows in not_eligible_heap (NEH), S_i > V.
		 * So when we need to compute max(V, min(S_i)) forall i in SCH+NEH,
		 * we only need to look into NEH.
		 */
		if (DN_KEY_GT(q->S, pipe->V)) {	/* Not eligible */
			if (pipe->scheduler_heap.elements == 0)
				kprintf("++ ouch! not eligible but empty scheduler!\n");
			heap_insert(&pipe->not_eligible_heap, q->S, q);
		} else {
			heap_insert(&pipe->scheduler_heap, q->F, q);
			if (pipe->numbytes >= 0) {	/* Pipe is idle */
				if (pipe->scheduler_heap.elements != 1)
					kprintf("*** OUCH! pipe should have been idle!\n");
				IPFW3_DEBUG("Waking up pipe %d at %d\n",
						pipe->pipe_nr, (int)(q->F >> MY_M));
				pipe->sched_time = curr_time;
				ready_event_wfq(pipe);
			}
		}
	}
done:
	return 0;

dropit:
	if (q)
		q->drops++;
	return ENOBUFS;
}

/*
 * Dispose all packets and flow_queues on a flow_set.
 * If all=1, also remove red lookup table and other storage,
 * including the descriptor itself.
 * For the one in dn_pipe MUST also cleanup ready_heap...
 */
static void
purge_flow_set(struct dn_flow_set *fs, int all)
{
	int i;
#ifdef INVARIANTS
	int rq_elements = 0;
#endif

	for (i = 0; i <= fs->rq_size; i++) {
		struct dn_flow_queue *q;

		while ((q = LIST_FIRST(&fs->rq[i])) != NULL) {
			struct dn_pkt *pkt;

			while ((pkt = TAILQ_FIRST(&q->queue)) != NULL) {
				TAILQ_REMOVE(&q->queue, pkt, dn_next);
				ip_dn_packet_free(pkt);
			}

			LIST_REMOVE(q, q_link);
			kfree(q, M_DUMMYNET);

#ifdef INVARIANTS
			rq_elements++;
#endif
		}
	}
	KASSERT(rq_elements == fs->rq_elements,
			("# rq elements mismatch, freed %d, total %d",
			 rq_elements, fs->rq_elements));
	fs->rq_elements = 0;

	if (all) {
		/* RED - free lookup table */
		if (fs->w_q_lookup)
			kfree(fs->w_q_lookup, M_DUMMYNET);

		if (fs->rq)
			kfree(fs->rq, M_DUMMYNET);

		/*
		 * If this fs is not part of a pipe, free it
		 *
		 * fs->pipe == NULL could happen, if 'fs' is a WF2Q and
		 * - No packet belongs to that flow set is delivered by
		 *   dummynet_io(), i.e. parent pipe is not installed yet.
		 * - Parent pipe is deleted.
		 */
		if (fs->pipe == NULL || (fs->pipe && fs != &fs->pipe->fs))
			kfree(fs, M_DUMMYNET);
	}
}

/*
 * Dispose all packets queued on a pipe (not a flow_set).
 * Also free all resources associated to a pipe, which is about
 * to be deleted.
 */
static void
purge_pipe(struct dn_pipe *pipe)
{
	struct dn_pkt *pkt;

	purge_flow_set(&pipe->fs, 1);

	while ((pkt = TAILQ_FIRST(&pipe->p_queue)) != NULL) {
		TAILQ_REMOVE(&pipe->p_queue, pkt, dn_next);
		ip_dn_packet_free(pkt);
	}

	heap_free(&pipe->scheduler_heap);
	heap_free(&pipe->not_eligible_heap);
	heap_free(&pipe->idle_heap);
}

/*
 * Delete all pipes and heaps returning memory.
 */
static void
dummynet_flush(void)
{
	struct dn_pipe_head pipe_list;
	struct dn_flowset_head fs_list;
	struct dn_pipe *p;
	struct dn_flow_set *fs;
	int i;

	/*
	 * Prevent future matches...
	 */
	LIST_INIT(&pipe_list);
	for (i = 0; i < DN_NR_HASH_MAX; ++i) {
		struct dn_pipe_head *pipe_hdr = &pipe_table[i];

		while ((p = LIST_FIRST(pipe_hdr)) != NULL) {
			LIST_REMOVE(p, p_link);
			LIST_INSERT_HEAD(&pipe_list, p, p_link);
		}
	}

	LIST_INIT(&fs_list);
	for (i = 0; i < DN_NR_HASH_MAX; ++i) {
		struct dn_flowset_head *fs_hdr = &flowset_table[i];

		while ((fs = LIST_FIRST(fs_hdr)) != NULL) {
			LIST_REMOVE(fs, fs_link);
			LIST_INSERT_HEAD(&fs_list, fs, fs_link);
		}
	}

	/* Free heaps so we don't have unwanted events */
	heap_free(&ready_heap);
	heap_free(&wfq_ready_heap);
	heap_free(&extract_heap);

	/*
	 * Now purge all queued pkts and delete all pipes
	 */
	/* Scan and purge all flow_sets. */
	while ((fs = LIST_FIRST(&fs_list)) != NULL) {
		LIST_REMOVE(fs, fs_link);
		purge_flow_set(fs, 1);
	}

	while ((p = LIST_FIRST(&pipe_list)) != NULL) {
		LIST_REMOVE(p, p_link);
		purge_pipe(p);
		kfree(p, M_DUMMYNET);
	}
}

/*
 * setup RED parameters
 */
static int
config_red(const struct dn_ioc_flowset *ioc_fs, struct dn_flow_set *x)
{
	int i;

	x->w_q = ioc_fs->w_q;
	x->min_th = SCALE(ioc_fs->min_th);
	x->max_th = SCALE(ioc_fs->max_th);
	x->max_p = ioc_fs->max_p;

	x->c_1 = ioc_fs->max_p / (ioc_fs->max_th - ioc_fs->min_th);
	x->c_2 = SCALE_MUL(x->c_1, SCALE(ioc_fs->min_th));
	if (x->flags_fs & DN_IS_GENTLE_RED) {
		x->c_3 = (SCALE(1) - ioc_fs->max_p) / ioc_fs->max_th;
		x->c_4 = (SCALE(1) - 2 * ioc_fs->max_p);
	}

	/* If the lookup table already exist, free and create it again */
	if (x->w_q_lookup) {
		kfree(x->w_q_lookup, M_DUMMYNET);
		x->w_q_lookup = NULL ;
	}

	if (red_lookup_depth == 0) {
		kprintf("net.inet.ip.dummynet.red_lookup_depth must be > 0\n");
		kfree(x, M_DUMMYNET);
		return EINVAL;
	}
	x->lookup_depth = red_lookup_depth;
	x->w_q_lookup = kmalloc(x->lookup_depth * sizeof(int),
			M_DUMMYNET, M_WAITOK);

	/* Fill the lookup table with (1 - w_q)^x */
	x->lookup_step = ioc_fs->lookup_step;
	x->lookup_weight = ioc_fs->lookup_weight;

	x->w_q_lookup[0] = SCALE(1) - x->w_q;
	for (i = 1; i < x->lookup_depth; i++)
		x->w_q_lookup[i] = SCALE_MUL(x->w_q_lookup[i - 1], x->lookup_weight);

	if (red_avg_pkt_size < 1)
		red_avg_pkt_size = 512;
	x->avg_pkt_size = red_avg_pkt_size;

	if (red_max_pkt_size < 1)
		red_max_pkt_size = 1500;
	x->max_pkt_size = red_max_pkt_size;

	return 0;
}

static void
alloc_hash(struct dn_flow_set *x, const struct dn_ioc_flowset *ioc_fs)
{
	int i, alloc_size;

	if (x->flags_fs & DN_HAVE_FLOW_MASK) {
		int l = ioc_fs->rq_size;

		/* Allocate some slots */
		if (l == 0)
			l = dn_hash_size;

		if (l < DN_MIN_HASH_SIZE)
			l = DN_MIN_HASH_SIZE;
		else if (l > DN_MAX_HASH_SIZE)
			l = DN_MAX_HASH_SIZE;

		x->rq_size = l;
	} else {
		/* One is enough for null mask */
		x->rq_size = 1;
	}
	alloc_size = x->rq_size + 1;

	x->rq = kmalloc(alloc_size * sizeof(struct dn_flowqueue_head),
			M_DUMMYNET, M_WAITOK | M_ZERO);
	x->rq_elements = 0;

	for (i = 0; i < alloc_size; ++i)
		LIST_INIT(&x->rq[i]);
}

static void
set_flowid_parms(struct dn_flow_id *id, const struct dn_ioc_flowid *ioc_id)
{
	id->fid_dst_ip = ioc_id->u.ip.dst_ip;
	id->fid_src_ip = ioc_id->u.ip.src_ip;
	id->fid_dst_port = ioc_id->u.ip.dst_port;
	id->fid_src_port = ioc_id->u.ip.src_port;
	id->fid_proto = ioc_id->u.ip.proto;
	id->fid_flags = ioc_id->u.ip.flags;
}

static void
set_fs_parms(struct dn_flow_set *x, const struct dn_ioc_flowset *ioc_fs)
{
	x->flags_fs = ioc_fs->flags_fs;
	x->qsize = ioc_fs->qsize;
	x->plr = ioc_fs->plr;
	set_flowid_parms(&x->flow_mask, &ioc_fs->flow_mask);
	if (x->flags_fs & DN_QSIZE_IS_BYTES) {
		if (x->qsize > 1024 * 1024)
			x->qsize = 1024 * 1024;
	} else {
		if (x->qsize == 0 || x->qsize > 100)
			x->qsize = 50;
	}

	/* Configuring RED */
	if (x->flags_fs & DN_IS_RED)
		config_red(ioc_fs, x);	/* XXX should check errors */
}

/*
 * setup pipe or queue parameters.
 */

static int
config_pipe(struct dn_ioc_pipe *ioc_pipe)
{
	struct dn_ioc_flowset *ioc_fs = &ioc_pipe->fs;
	int error;

	/*
	 * The config program passes parameters as follows:
	 * bw	bits/second (0 means no limits)
	 * delay	ms (must be translated into ticks)
	 * qsize	slots or bytes
	 */
	ioc_pipe->delay = (ioc_pipe->delay * dn_hz) / 1000;

	/*
	 * We need either a pipe number or a flow_set number
	 */
	if (ioc_pipe->pipe_nr == 0 && ioc_fs->fs_nr == 0)
		return EINVAL;
	if (ioc_pipe->pipe_nr != 0 && ioc_fs->fs_nr != 0)
		return EINVAL;

	/*
	 * Validate pipe number
	 */
	if (ioc_pipe->pipe_nr > DN_PIPE_NR_MAX || ioc_pipe->pipe_nr < 0)
		return EINVAL;

	error = EINVAL;
	if (ioc_pipe->pipe_nr != 0) {	/* This is a pipe */
		struct dn_pipe *x, *p;

		/* Locate pipe */
		p = dn_find_pipe(ioc_pipe->pipe_nr);

		if (p == NULL) {	/* New pipe */
			x = kmalloc(sizeof(struct dn_pipe), M_DUMMYNET, M_WAITOK | M_ZERO);
			x->pipe_nr = ioc_pipe->pipe_nr;
			x->fs.pipe = x;
			TAILQ_INIT(&x->p_queue);

			/*
			 * idle_heap is the only one from which we extract from the middle.
			 */
			x->idle_heap.size = x->idle_heap.elements = 0;
			x->idle_heap.offset = __offsetof(struct dn_flow_queue, heap_pos);
		} else {
			int i;

			x = p;

			/* Flush accumulated credit for all queues */
			for (i = 0; i <= x->fs.rq_size; i++) {
				struct dn_flow_queue *q;

				LIST_FOREACH(q, &x->fs.rq[i], q_link)
					q->numbytes = 0;
			}
		}

		x->bandwidth = ioc_pipe->bandwidth;
		x->numbytes = 0; /* Just in case... */
		x->delay = ioc_pipe->delay;

		set_fs_parms(&x->fs, ioc_fs);

		if (x->fs.rq == NULL) {	/* A new pipe */
			struct dn_pipe_head *pipe_hdr;

			alloc_hash(&x->fs, ioc_fs);

			pipe_hdr = &pipe_table[DN_NR_HASH(x->pipe_nr)];
			LIST_INSERT_HEAD(pipe_hdr, x, p_link);
		}
	} else {	/* Config flow_set */
		struct dn_flow_set *x, *fs;

		/* Locate flow_set */
		fs = dn_find_flowset(ioc_fs->fs_nr);

		if (fs == NULL) {	/* New flow_set */
			if (ioc_fs->parent_nr == 0)	/* Need link to a pipe */
				goto back;

			x = kmalloc(sizeof(struct dn_flow_set), M_DUMMYNET,
					M_WAITOK | M_ZERO);
			x->fs_nr = ioc_fs->fs_nr;
			x->parent_nr = ioc_fs->parent_nr;
			x->weight = ioc_fs->weight;
			if (x->weight == 0)
				x->weight = 1;
			else if (x->weight > 100)
				x->weight = 100;
		} else {
			/* Change parent pipe not allowed; must delete and recreate */
			if (ioc_fs->parent_nr != 0 && fs->parent_nr != ioc_fs->parent_nr)
				goto back;
			x = fs;
		}

		set_fs_parms(x, ioc_fs);

		if (x->rq == NULL) {	/* A new flow_set */
			struct dn_flowset_head *fs_hdr;

			alloc_hash(x, ioc_fs);

			fs_hdr = &flowset_table[DN_NR_HASH(x->fs_nr)];
			LIST_INSERT_HEAD(fs_hdr, x, fs_link);
		}
	}
	error = 0;

back:
	return error;
}

/*
 * Helper function to remove from a heap queues which are linked to
 * a flow_set about to be deleted.
 */
static void
fs_remove_from_heap(struct dn_heap *h, struct dn_flow_set *fs)
{
	int i = 0, found = 0;

	while (i < h->elements) {
		if (((struct dn_flow_queue *)h->p[i].object)->fs == fs) {
			h->elements--;
			h->p[i] = h->p[h->elements];
			found++;
		} else {
			i++;
		}
	}
	if (found)
		heapify(h);
}

/*
 * helper function to remove a pipe from a heap (can be there at most once)
 */
static void
pipe_remove_from_heap(struct dn_heap *h, struct dn_pipe *p)
{
	if (h->elements > 0) {
		int i;

		for (i = 0; i < h->elements; i++) {
			if (h->p[i].object == p) { /* found it */
				h->elements--;
				h->p[i] = h->p[h->elements];
				heapify(h);
				break;
			}
		}
	}
}

static void
dn_unref_pipe_cb(struct dn_flow_set *fs, void *pipe0)
{
	struct dn_pipe *pipe = pipe0;

	if (fs->pipe == pipe) {
		kprintf("++ ref to pipe %d from fs %d\n",
				pipe->pipe_nr, fs->fs_nr);
		fs->pipe = NULL;
		purge_flow_set(fs, 0);
	}
}

/*
 * Fully delete a pipe or a queue, cleaning up associated info.
 */
static int
delete_pipe(const struct dn_ioc_pipe *ioc_pipe)
{
	struct dn_pipe *p;
	int error;

	if (ioc_pipe->pipe_nr == 0 && ioc_pipe->fs.fs_nr == 0)
		return EINVAL;
	if (ioc_pipe->pipe_nr != 0 && ioc_pipe->fs.fs_nr != 0)
		return EINVAL;

	if (ioc_pipe->pipe_nr > DN_NR_HASH_MAX || ioc_pipe->pipe_nr < 0)
		return EINVAL;

	error = EINVAL;
	if (ioc_pipe->pipe_nr != 0) {	/* This is an old-style pipe */
		/* Locate pipe */
		p = dn_find_pipe(ioc_pipe->pipe_nr);
		if (p == NULL)
			goto back; /* Not found */

		/* Unlink from pipe hash table */
		LIST_REMOVE(p, p_link);

		/* Remove all references to this pipe from flow_sets */
		dn_iterate_flowset(dn_unref_pipe_cb, p);

		fs_remove_from_heap(&ready_heap, &p->fs);
		purge_pipe(p);	/* Remove all data associated to this pipe */

		/* Remove reference to here from extract_heap and wfq_ready_heap */
		pipe_remove_from_heap(&extract_heap, p);
		pipe_remove_from_heap(&wfq_ready_heap, p);

		kfree(p, M_DUMMYNET);
	} else {	/* This is a WF2Q queue (dn_flow_set) */
		struct dn_flow_set *fs;

		/* Locate flow_set */
		fs = dn_find_flowset(ioc_pipe->fs.fs_nr);
		if (fs == NULL)
			goto back; /* Not found */

		LIST_REMOVE(fs, fs_link);

		if ((p = fs->pipe) != NULL) {
			/* Update total weight on parent pipe and cleanup parent heaps */
			p->sum -= fs->weight * fs->backlogged;
			fs_remove_from_heap(&p->not_eligible_heap, fs);
			fs_remove_from_heap(&p->scheduler_heap, fs);
#if 1	/* XXX should i remove from idle_heap as well ? */
			fs_remove_from_heap(&p->idle_heap, fs);
#endif
		}
		purge_flow_set(fs, 1);
	}
	error = 0;

back:
	return error;
}

/*
 * helper function used to copy data from kernel in DUMMYNET_GET
 */
static void
dn_copy_flowid(const struct dn_flow_id *id, struct dn_ioc_flowid *ioc_id)
{
	ioc_id->type = ETHERTYPE_IP;
	ioc_id->u.ip.dst_ip = id->fid_dst_ip;
	ioc_id->u.ip.src_ip = id->fid_src_ip;
	ioc_id->u.ip.dst_port = id->fid_dst_port;
	ioc_id->u.ip.src_port = id->fid_src_port;
	ioc_id->u.ip.proto = id->fid_proto;
	ioc_id->u.ip.flags = id->fid_flags;
}

static void *
dn_copy_flowqueues(const struct dn_flow_set *fs, void *bp)
{
	struct dn_ioc_flowqueue *ioc_fq = bp;
	int i, copied = 0;

	for (i = 0; i <= fs->rq_size; i++) {
		const struct dn_flow_queue *q;

		LIST_FOREACH(q, &fs->rq[i], q_link) {
			if (q->hash_slot != i) {	/* XXX ASSERT */
				kprintf("++ at %d: wrong slot (have %d, "
						"should be %d)\n",
						copied, q->hash_slot, i);
			}
			if (q->fs != fs) {		/* XXX ASSERT */
				kprintf("++ at %d: wrong fs ptr (have %p, should be %p)\n",
						i, q->fs, fs);
			}

			copied++;

			ioc_fq->len = q->len;
			ioc_fq->len_bytes = q->len_bytes;
			ioc_fq->tot_pkts = q->tot_pkts;
			ioc_fq->tot_bytes = q->tot_bytes;
			ioc_fq->drops = q->drops;
			ioc_fq->hash_slot = q->hash_slot;
			ioc_fq->S = q->S;
			ioc_fq->F = q->F;
			dn_copy_flowid(&q->id, &ioc_fq->id);

			ioc_fq++;
		}
	}

	if (copied != fs->rq_elements) {	/* XXX ASSERT */
		kprintf("++ wrong count, have %d should be %d\n",
				copied, fs->rq_elements);
	}
	return ioc_fq;
}

static void
dn_copy_flowset(const struct dn_flow_set *fs, struct dn_ioc_flowset *ioc_fs,
		u_short fs_type)
{
	ioc_fs->fs_type = fs_type;

	ioc_fs->fs_nr = fs->fs_nr;
	ioc_fs->flags_fs = fs->flags_fs;
	ioc_fs->parent_nr = fs->parent_nr;

	ioc_fs->weight = fs->weight;
	ioc_fs->qsize = fs->qsize;
	ioc_fs->plr = fs->plr;

	ioc_fs->rq_size = fs->rq_size;
	ioc_fs->rq_elements = fs->rq_elements;

	ioc_fs->w_q = fs->w_q;
	ioc_fs->max_th = fs->max_th;
	ioc_fs->min_th = fs->min_th;
	ioc_fs->max_p = fs->max_p;

	dn_copy_flowid(&fs->flow_mask, &ioc_fs->flow_mask);
}

static void
dn_calc_pipe_size_cb(struct dn_pipe *pipe, void *sz)
{
	size_t *size = sz;

	*size += sizeof(struct dn_ioc_pipe) +
		pipe->fs.rq_elements * sizeof(struct dn_ioc_flowqueue);
}

static void
dn_calc_fs_size_cb(struct dn_flow_set *fs, void *sz)
{
	size_t *size = sz;

	*size += sizeof(struct dn_ioc_flowset) +
		fs->rq_elements * sizeof(struct dn_ioc_flowqueue);
}

static void
dn_copyout_pipe_cb(struct dn_pipe *pipe, void *bp0)
{
	char **bp = bp0;
	struct dn_ioc_pipe *ioc_pipe = (struct dn_ioc_pipe *)(*bp);

	/*
	 * Copy flow set descriptor associated with this pipe
	 */
	dn_copy_flowset(&pipe->fs, &ioc_pipe->fs, DN_IS_PIPE);

	/*
	 * Copy pipe descriptor
	 */
	ioc_pipe->bandwidth = pipe->bandwidth;
	ioc_pipe->pipe_nr = pipe->pipe_nr;
	ioc_pipe->V = pipe->V;
	/* Convert delay to milliseconds */
	ioc_pipe->delay = (pipe->delay * 1000) / dn_hz;

	/*
	 * Copy flow queue descriptors
	 */
	*bp += sizeof(*ioc_pipe);
	*bp = dn_copy_flowqueues(&pipe->fs, *bp);
}

static void
dn_copyout_fs_cb(struct dn_flow_set *fs, void *bp0)
{
	char **bp = bp0;
	struct dn_ioc_flowset *ioc_fs = (struct dn_ioc_flowset *)(*bp);

	/*
	 * Copy flow set descriptor
	 */
	dn_copy_flowset(fs, ioc_fs, DN_IS_QUEUE);

	/*
	 * Copy flow queue descriptors
	 */
	*bp += sizeof(*ioc_fs);
	*bp = dn_copy_flowqueues(fs, *bp);
}

static int
dummynet_get(struct dn_sopt *dn_sopt)
{
	char *buf, *bp;
	size_t size = 0;

	/*
	 * Compute size of data structures: list of pipes and flow_sets.
	 */
	dn_iterate_pipe(dn_calc_pipe_size_cb, &size);
	dn_iterate_flowset(dn_calc_fs_size_cb, &size);

	/*
	 * Copyout pipe/flow_set/flow_queue
	 */
	bp = buf = kmalloc(size, M_TEMP, M_WAITOK | M_ZERO);
	dn_iterate_pipe(dn_copyout_pipe_cb, &bp);
	dn_iterate_flowset(dn_copyout_fs_cb, &bp);

	/* Temp memory will be freed by caller */
	dn_sopt->dn_sopt_arg = buf;
	dn_sopt->dn_sopt_arglen = size;
	return 0;
}

/*
 * Handler for the various dummynet socket options (get, flush, config, del)
 */
static int
dummynet_ctl(struct dn_sopt *dn_sopt)
{
	int error = 0;

	switch (dn_sopt->dn_sopt_name) {
		case IP_DUMMYNET_GET:
			error = dummynet_get(dn_sopt);
			break;

		case IP_DUMMYNET_FLUSH:
			dummynet_flush();
			break;

		case IP_DUMMYNET_CONFIGURE:
			KKASSERT(dn_sopt->dn_sopt_arglen == sizeof(struct dn_ioc_pipe));
			error = config_pipe(dn_sopt->dn_sopt_arg);
			break;

		case IP_DUMMYNET_DEL:	/* Remove a pipe or flow_set */
			KKASSERT(dn_sopt->dn_sopt_arglen == sizeof(struct dn_ioc_pipe));
			error = delete_pipe(dn_sopt->dn_sopt_arg);
			break;

		default:
			kprintf("%s -- unknown option %d\n", __func__, dn_sopt->dn_sopt_name);
			error = EINVAL;
			break;
	}
	return error;
}

static void
dummynet_clock(systimer_t info __unused, int in_ipi __unused,
	struct intrframe *frame __unused)
{
	KASSERT(mycpuid == ip_dn_cpu,
			("dummynet systimer comes on cpu%d, should be %d!",
			 mycpuid, ip_dn_cpu));

	crit_enter();
	if (DUMMYNET_LOADED && (dn_netmsg.lmsg.ms_flags & MSGF_DONE))
		lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), &dn_netmsg.lmsg);
	crit_exit();
}

static int
sysctl_dn_hz(SYSCTL_HANDLER_ARGS)
{
	int error, val;

	val = dn_hz;
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || req->newptr == NULL)
		return error;
	if (val <= 0)
		return EINVAL;
	else if (val > DN_CALLOUT_FREQ_MAX)
		val = DN_CALLOUT_FREQ_MAX;

	crit_enter();
	dn_hz = val;
	systimer_adjust_periodic(&dn_clock, val);
	crit_exit();

	return 0;
}

static void
ip_dn_init_dispatch(netmsg_t msg)
{
	int i, error = 0;

	KASSERT(mycpuid == ip_dn_cpu,
			("%s runs on cpu%d, instead of cpu%d", __func__,
			 mycpuid, ip_dn_cpu));

	crit_enter();

	if (DUMMYNET_LOADED) {
		kprintf("DUMMYNET already loaded\n");
		error = EEXIST;
		goto back;
	}

	kprintf("DUMMYNET initialized (011031)\n");

	for (i = 0; i < DN_NR_HASH_MAX; ++i)
		LIST_INIT(&pipe_table[i]);

	for (i = 0; i < DN_NR_HASH_MAX; ++i)
		LIST_INIT(&flowset_table[i]);

	ready_heap.size = ready_heap.elements = 0;
	ready_heap.offset = 0;

	wfq_ready_heap.size = wfq_ready_heap.elements = 0;
	wfq_ready_heap.offset = 0;

	extract_heap.size = extract_heap.elements = 0;
	extract_heap.offset = 0;

	ip_dn_ctl_ptr = dummynet_ctl;
	ip_dn_io_ptr = dummynet_io;

	netmsg_init(&dn_netmsg, NULL, &netisr_adone_rport,
			0, dummynet);
	systimer_init_periodic_nq(&dn_clock, dummynet_clock, NULL, dn_hz);

back:
	crit_exit();
	lwkt_replymsg(&msg->lmsg, error);
}

static int
ip_dn_init(void)
{
	struct netmsg_base smsg;

	if (ip_dn_cpu >= ncpus) {
		kprintf("%s: CPU%d does not exist, switch to CPU0\n",
				__func__, ip_dn_cpu);
		ip_dn_cpu = 0;
	}

	ip_fw3_register_module(MODULE_DUMMYNET_ID, MODULE_DUMMYNET_NAME);
	ip_fw3_register_filter_funcs(MODULE_DUMMYNET_ID, O_DUMMYNET_PIPE,
			(filter_func)check_pipe);
	ip_fw3_register_filter_funcs(MODULE_DUMMYNET_ID, O_DUMMYNET_QUEUE,
			(filter_func)check_pipe);

	netmsg_init(&smsg, NULL, &curthread->td_msgport,
			0, ip_dn_init_dispatch);
	lwkt_domsg(netisr_cpuport(ip_dn_cpu), &smsg.lmsg, 0);
	return smsg.lmsg.ms_error;
}

#ifdef KLD_MODULE

static void
ip_dn_stop_dispatch(netmsg_t msg)
{
	crit_enter();

	dummynet_flush();

	ip_dn_ctl_ptr = NULL;
	ip_dn_io_ptr = NULL;

	systimer_del(&dn_clock);

	crit_exit();
	lwkt_replymsg(&msg->lmsg, 0);
}

static void
ip_dn_stop(void)
{
	struct netmsg_base smsg;

	netmsg_init(&smsg, NULL, &curthread->td_msgport,
			0, ip_dn_stop_dispatch);
	lwkt_domsg(netisr_cpuport(ip_dn_cpu), &smsg.lmsg, 0);

	netmsg_service_sync();
}

#endif	/* KLD_MODULE */

static int
dummynet_modevent(module_t mod, int type, void *data)
{
	switch (type) {
		case MOD_LOAD:
			return ip_dn_init();

		case MOD_UNLOAD:
#ifndef KLD_MODULE
			kprintf("dummynet statically compiled, cannot unload\n");
			return EINVAL;
#else
			ip_dn_stop();
#endif
			break;

		default:
			break;
	}
	return 0;
}

static moduledata_t dummynet_mod = {
	"dummynet2",
	dummynet_modevent,
	NULL
};
DECLARE_MODULE(dummynet3, dummynet_mod, SI_SUB_PROTO_END, SI_ORDER_ANY);
MODULE_DEPEND(dummynet3, ipfw3_basic, 1, 1, 1);
MODULE_VERSION(dummynet3, 1);