summaryrefslogtreecommitdiffstats
path: root/src/vnet/ip/reass/ip6_full_reass.c
blob: 74193346c20f321c557eaab8f5e2d32a3583ce64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file
 * @brief IPv6 Full Reassembly.
 *
 * This file contains the source code for IPv6 full reassembly.
 */

#include <vppinfra/vec.h>
#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vppinfra/bihash_48_8.h>
#include <vnet/ip/reass/ip6_full_reass.h>
#include <vnet/ip/ip6_inlines.h>

#define MSEC_PER_SEC 1000
#define IP6_FULL_REASS_TIMEOUT_DEFAULT_MS 200
/* As there are only 1024 reass context per thread, either the DDOS attacks
 * or fractions of real timeouts, would consume these contexts quickly and
 * running out context space and unable to perform reassembly */
#define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 50 // 50 ms default
#define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
#define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
#define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)

typedef enum
{
  IP6_FULL_REASS_RC_OK,
  IP6_FULL_REASS_RC_INTERNAL_ERROR,
  IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
  IP6_FULL_REASS_RC_NO_BUF,
  IP6_FULL_REASS_RC_HANDOFF,
  IP6_FULL_REASS_RC_INVALID_FRAG_LEN,
  IP6_FULL_REASS_RC_OVERLAP,
} ip6_full_reass_rc_t;

typedef struct
{
  union
  {
    struct
    {
      ip6_address_t src;
      ip6_address_t dst;
      u32 xx_id;
      u32 frag_id;
      u8 unused[7];
      u8 proto;
    };
    u64 as_u64[6];
  };
} ip6_full_reass_key_t;

typedef union
{
  struct
  {
    u32 reass_index;
    u32 memory_owner_thread_index;
  };
  u64 as_u64;
} ip6_full_reass_val_t;

typedef union
{
  struct
  {
    ip6_full_reass_key_t k;
    ip6_full_reass_val_t v;
  };
  clib_bihash_kv_48_8_t kv;
} ip6_full_reass_kv_t;


always_inline u32
ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
{
  vnet_buffer_opaque_t *vnb = vnet_buffer (b);
  return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
}

always_inline u16
ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
{
  vnet_buffer_opaque_t *vnb = vnet_buffer (b);
  return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
    (vnb->ip.reass.fragment_first +
     ip6_full_reass_buffer_get_data_offset (b)) + 1;
}

typedef struct
{
  // hash table key
  ip6_full_reass_key_t key;
  // time when last packet was received
  f64 last_heard;
  // internal id of this reassembly
  u64 id;
  // buffer index of first buffer in this reassembly context
  u32 first_bi;
  // last octet of packet, ~0 until fragment without more_fragments arrives
  u32 last_packet_octet;
  // length of data collected so far
  u32 data_len;
  // trace operation counter
  u32 trace_op_counter;
  // next index - used by custom apps (~0 if not set)
  u32 next_index;
  // error next index - used by custom apps (~0 if not set)
  u32 error_next_index;
  // minimum fragment length for this reassembly - used to estimate MTU
  u16 min_fragment_length;
  // number of fragments for this reassembly
  u32 fragments_n;
  // thread owning memory for this context (whose pool contains this ctx)
  u32 memory_owner_thread_index;
  // thread which received fragment with offset 0 and which sends out the
  // completed reassembly
  u32 sendout_thread_index;
} ip6_full_reass_t;

typedef struct
{
  ip6_full_reass_t *pool;
  u32 reass_n;
  u32 id_counter;
  // for pacing the main thread timeouts
  u32 last_id;
  clib_spinlock_t lock;
} ip6_full_reass_per_thread_t;

typedef struct
{
  // IPv6 config
  u32 timeout_ms;
  f64 timeout;
  u32 expire_walk_interval_ms;
  // maximum number of fragments in one reassembly
  u32 max_reass_len;
  // maximum number of reassemblies
  u32 max_reass_n;

  // IPv6 runtime
  clib_bihash_48_8_t hash;

  // per-thread data
  ip6_full_reass_per_thread_t *per_thread_data;

  // convenience
  vlib_main_t *vlib_main;

  u32 ip6_icmp_error_idx;
  u32 ip6_full_reass_expire_node_idx;

  /** Worker handoff */
  u32 fq_index;
  u32 fq_local_index;
  u32 fq_feature_index;
  u32 fq_custom_index;

  // reference count for enabling/disabling feature - per interface
  u32 *feature_use_refcount_per_intf;

  // whether local fragmented packets are reassembled or not
  int is_local_reass_enabled;
} ip6_full_reass_main_t;

extern ip6_full_reass_main_t ip6_full_reass_main;

#ifndef CLIB_MARCH_VARIANT
ip6_full_reass_main_t ip6_full_reass_main;
#endif /* CLIB_MARCH_VARIANT */

typedef enum
{
  IP6_FULL_REASSEMBLY_NEXT_INPUT,
  IP6_FULL_REASSEMBLY_NEXT_DROP,
  IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
  IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
  IP6_FULL_REASSEMBLY_N_NEXT,
} ip6_full_reass_next_t;

typedef enum
{
  NORMAL,
  FEATURE,
  CUSTOM
} ip6_full_reass_node_type_t;

typedef enum
{
  RANGE_NEW,
  RANGE_DISCARD,
  RANGE_OVERLAP,
  ICMP_ERROR_RT_EXCEEDED,
  ICMP_ERROR_FL_TOO_BIG,
  ICMP_ERROR_FL_NOT_MULT_8,
  FINALIZE,
  HANDOFF,
  PASSTHROUGH,
} ip6_full_reass_trace_operation_e;

typedef struct
{
  u16 range_first;
  u16 range_last;
  u32 range_bi;
  i32 data_offset;
  u32 data_len;
  u32 first_bi;
} ip6_full_reass_range_trace_t;

typedef struct
{
  ip6_full_reass_trace_operation_e action;
  u32 reass_id;
  ip6_full_reass_range_trace_t trace_range;
  u32 op_id;
  u32 fragment_first;
  u32 fragment_last;
  u32 total_data_len;
  u32 thread_id;
  u32 thread_id_to;
  bool is_after_handoff;
  ip6_header_t ip6_header;
  ip6_frag_hdr_t ip6_frag_header;
} ip6_full_reass_trace_t;

static void
ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
			      ip6_full_reass_range_trace_t * trace)
{
  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
  vnet_buffer_opaque_t *vnb = vnet_buffer (b);
  trace->range_first = vnb->ip.reass.range_first;
  trace->range_last = vnb->ip.reass.range_last;
  trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
  trace->data_len = ip6_full_reass_buffer_get_data_len (b);
  trace->range_bi = bi;
}

static u8 *
format_ip6_full_reass_range_trace (u8 * s, va_list * args)
{
  ip6_full_reass_range_trace_t *trace =
    va_arg (*args, ip6_full_reass_range_trace_t *);
  s =
    format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
	    trace->range_last, trace->data_offset, trace->data_len,
	    trace->range_bi);
  return s;
}

static u8 *
format_ip6_full_reass_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
  u32 indent = 0;
  if (~0 != t->reass_id)
    {
      if (t->is_after_handoff)
	{
	  s =
	    format (s, "%U\n", format_ip6_header, &t->ip6_header,
		    sizeof (t->ip6_header));
	  s =
	    format (s, "  %U\n", format_ip6_frag_hdr, &t->ip6_frag_header,
		    sizeof (t->ip6_frag_header));
	  indent = 2;
	}
      s =
	format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
		t->reass_id, t->op_id);
      indent = format_get_indent (s);
      s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
		  t->trace_range.first_bi, t->total_data_len,
		  t->fragment_first, t->fragment_last);
    }
  switch (t->action)
    {
    case RANGE_NEW:
      s = format (s, "\n%Unew %U", format_white_space, indent,
		  format_ip6_full_reass_range_trace, &t->trace_range);
      break;
    case RANGE_DISCARD:
      s = format (s, "\n%Udiscard %U", format_white_space, indent,
		  format_ip6_full_reass_range_trace, &t->trace_range);
      break;
    case RANGE_OVERLAP:
      s = format (s, "\n%Uoverlap %U", format_white_space, indent,
		  format_ip6_full_reass_range_trace, &t->trace_range);
      break;
    case ICMP_ERROR_FL_TOO_BIG:
      s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
		  format_white_space, indent,
		  format_ip6_full_reass_range_trace, &t->trace_range);
      break;
    case ICMP_ERROR_FL_NOT_MULT_8:
      s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
		  format_white_space, indent,
		  format_ip6_full_reass_range_trace, &t->trace_range);
      break;
    case ICMP_ERROR_RT_EXCEEDED:
      s = format (s, "\n%Uicmp-error - reassembly time exceeded",
		  format_white_space, indent);
      break;
    case FINALIZE:
      s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
      break;
    case HANDOFF:
      s =
	format (s, "handoff from thread #%u to thread #%u", t->thread_id,
		t->thread_id_to);
      break;
    case PASSTHROUGH:
      s = format (s, "passthrough - not a fragment");
      break;
    }
  return s;
}

static void
ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
			  ip6_full_reass_t * reass, u32 bi,
			  ip6_frag_hdr_t * ip6_frag_header,
			  ip6_full_reass_trace_operation_e action,
			  u32 thread_id_to)
{
  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
  vnet_buffer_opaque_t *vnb = vnet_buffer (b);
  bool is_after_handoff = false;
  if (pool_is_free_index
      (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
    {
      // this buffer's trace is gone
      b->flags &= ~VLIB_BUFFER_IS_TRACED;
      return;
    }
  if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
    {
      is_after_handoff = true;
    }
  ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
  t->is_after_handoff = is_after_handoff;
  if (t->is_after_handoff)
    {
      clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
		   clib_min (sizeof (t->ip6_header), b->current_length));
      if (ip6_frag_header)
	{
	  clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
		       sizeof (t->ip6_frag_header));
	}
      else
	{
	  clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
	}
    }
  if (reass)
    {
      t->reass_id = reass->id;
      t->op_id = reass->trace_op_counter;
      t->trace_range.first_bi = reass->first_bi;
      t->total_data_len = reass->data_len;
      ++reass->trace_op_counter;
    }
  else
    {
      t->reass_id = ~0;
    }
  t->action = action;
  t->thread_id = vm->thread_index;
  t->thread_id_to = thread_id_to;
  ip6_full_reass_trace_details (vm, bi, &t->trace_range);
  t->fragment_first = vnb->ip.reass.fragment_first;
  t->fragment_last = vnb->ip.reass.fragment_last;
#if 0
  static u8 *s = NULL;
  s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
  printf ("%.*s\n", vec_len (s), s);
  fflush (stdout);
  vec_reset_length (s);
#endif
}

always_inline void
ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
			 ip6_full_reass_t * reass)
{
  pool_put (rt->pool, reass);
  --rt->reass_n;
}

always_inline void
ip6_full_reass_free (ip6_full_reass_main_t * rm,
		     ip6_full_reass_per_thread_t * rt,
		     ip6_full_reass_t * reass)
{
  clib_bihash_kv_48_8_t kv;
  kv.key[0] = reass->key.as_u64[0];
  kv.key[1] = reass->key.as_u64[1];
  kv.key[2] = reass->key.as_u64[2];
  kv.key[3] = reass->key.as_u64[3];
  kv.key[4] = reass->key.as_u64[4];
  kv.key[5] = reass->key.as_u64[5];
  clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
  ip6_full_reass_free_ctx (rt, reass);
}

/* n_left_to_next, and to_next are taken as input params, as this function
 * could be called from a graphnode, where its managing local copy of these
 * variables, and ignoring those and still trying to enqueue the buffers
 * with local variables would cause either buffer leak or corruption */
always_inline void
ip6_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
			 ip6_full_reass_t *reass, u32 *n_left_to_next,
			 u32 **to_next)
{
  u32 range_bi = reass->first_bi;
  vlib_buffer_t *range_b;
  vnet_buffer_opaque_t *range_vnb;
  u32 *to_free = NULL;

  while (~0 != range_bi)
    {
      range_b = vlib_get_buffer (vm, range_bi);
      range_vnb = vnet_buffer (range_b);

      if (~0 != range_bi)
	{
	  vec_add1 (to_free, range_bi);
	}
      range_bi = range_vnb->ip.reass.next_range_bi;
    }

  /* send to next_error_index */
  if (~0 != reass->error_next_index &&
      reass->error_next_index < node->n_next_nodes)
    {
      u32 next_index;

      next_index = reass->error_next_index;
      u32 bi = ~0;

      /* record number of packets sent to custom app */
      vlib_node_increment_counter (vm, node->node_index,
				   IP6_ERROR_REASS_TO_CUSTOM_APP,
				   vec_len (to_free));

      while (vec_len (to_free) > 0)
	{
	  vlib_get_next_frame (vm, node, next_index, *to_next,
			       (*n_left_to_next));

	  while (vec_len (to_free) > 0 && (*n_left_to_next) > 0)
	    {
	      bi = vec_pop (to_free);

	      if (~0 != bi)
		{
		  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
		  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
		    {
		      ip6_full_reass_add_trace (vm, node, reass, bi, NULL,
						RANGE_DISCARD, ~0);
		    }
		  *to_next[0] = bi;
		  (*to_next) += 1;
		  (*n_left_to_next) -= 1;
		}
	    }
	  vlib_put_next_frame (vm, node, next_index, (*n_left_to_next));
	}
    }
  else
    {
      vlib_buffer_free (vm, to_free, vec_len (to_free));
    }
  vec_free (to_free);
}

always_inline void
sanitize_reass_buffers_add_missing (vlib_main_t *vm, ip6_full_reass_t *reass,
				    u32 *bi0)
{
  u32 range_bi = reass->first_bi;
  vlib_buffer_t *range_b;
  vnet_buffer_opaque_t *range_vnb;

  while (~0 != range_bi)
    {
      range_b = vlib_get_buffer (vm, range_bi);
      range_vnb = vnet_buffer (range_b);
      u32 bi = range_bi;
      if (~0 != bi)
	{
	  if (bi == *bi0)
	    *bi0 = ~0;
	  if (range_b->flags & VLIB_BUFFER_NEXT_PRESENT)
	    {
	      u32 _bi = bi;
	      vlib_buffer_t *_b = vlib_get_buffer (vm, _bi);
	      while (_b->flags & VLIB_BUFFER_NEXT_PRESENT)
		{
		  if (_b->next_buffer != range_vnb->ip.reass.next_range_bi)
		    {
		      _bi = _b->next_buffer;
		      _b = vlib_get_buffer (vm, _bi);
		    }
		  else
		    {
		      _b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
		      break;
		    }
		}
	    }
	  range_bi = range_vnb->ip.reass.next_range_bi;
	}
    }
  if (*bi0 != ~0)
    {
      vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
      vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
      if (~0 != reass->first_bi)
	{
	  fvnb->ip.reass.next_range_bi = reass->first_bi;
	  reass->first_bi = *bi0;
	}
      else
	{
	  reass->first_bi = *bi0;
	  fvnb->ip.reass.next_range_bi = ~0;
	}
      *bi0 = ~0;
    }
}

always_inline void
ip6_full_reass_on_timeout (vlib_main_t *vm, vlib_node_runtime_t *node,
			   ip6_full_reass_t *reass, u32 *icmp_bi,
			   u32 *n_left_to_next, u32 **to_next)
{
  if (~0 == reass->first_bi)
    {
      return;
    }
  if (~0 == reass->next_index)	// custom apps don't want icmp
    {
      vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
      if (0 == vnet_buffer (b)->ip.reass.fragment_first)
	{
	  *icmp_bi = reass->first_bi;
	  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
	    {
	      ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
					ICMP_ERROR_RT_EXCEEDED, ~0);
	    }
	  // fragment with offset zero received - send icmp message back
	  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
	    {
	      // separate first buffer from chain and steer it towards icmp node
	      b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
	      reass->first_bi = b->next_buffer;
	    }
	  else
	    {
	      reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
	    }
	  icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
				       ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
				       0);
	}
    }
  ip6_full_reass_drop_all (vm, node, reass, n_left_to_next, to_next);
}

always_inline ip6_full_reass_t *
ip6_full_reass_find_or_create (vlib_main_t *vm, vlib_node_runtime_t *node,
			       ip6_full_reass_main_t *rm,
			       ip6_full_reass_per_thread_t *rt,
			       ip6_full_reass_kv_t *kv, u32 *icmp_bi,
			       u8 *do_handoff, int skip_bihash,
			       u32 *n_left_to_next, u32 **to_next)
{
  ip6_full_reass_t *reass;
  f64 now;

again:

  reass = NULL;
  now = vlib_time_now (vm);

  if (!skip_bihash && !clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
    {
      if (vm->thread_index != kv->v.memory_owner_thread_index)
	{
	  *do_handoff = 1;
	  return NULL;
	}

      reass =
	pool_elt_at_index (rm->per_thread_data
			   [kv->v.memory_owner_thread_index].pool,
			   kv->v.reass_index);

      if (now > reass->last_heard + rm->timeout)
	{
	  vlib_node_increment_counter (vm, node->node_index,
				       IP6_ERROR_REASS_TIMEOUT, 1);
	  ip6_full_reass_on_timeout (vm, node, reass, icmp_bi, n_left_to_next,
				     to_next);
	  ip6_full_reass_free (rm, rt, reass);
	  reass = NULL;
	}
    }

  if (reass)
    {
      reass->last_heard = now;
      return reass;
    }

  if (rt->reass_n >= rm->max_reass_n)
    {
      reass = NULL;
      return reass;
    }
  else
    {
      pool_get (rt->pool, reass);
      clib_memset (reass, 0, sizeof (*reass));
      reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
      ++rt->id_counter;
      reass->first_bi = ~0;
      reass->last_packet_octet = ~0;
      reass->data_len = 0;
      reass->next_index = ~0;
      reass->error_next_index = ~0;
      reass->memory_owner_thread_index = vm->thread_index;
      ++rt->reass_n;
    }

  kv->v.reass_index = (reass - rt->pool);
  kv->v.memory_owner_thread_index = vm->thread_index;
  reass->last_heard = now;

  if (!skip_bihash)
    {
      reass->key.as_u64[0] = kv->kv.key[0];
      reass->key.as_u64[1] = kv->kv.key[1];
      reass->key.as_u64[2] = kv->kv.key[2];
      reass->key.as_u64[3] = kv->kv.key[3];
      reass->key.as_u64[4] = kv->kv.key[4];
      reass->key.as_u64[5] = kv->kv.key[5];

      int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
      if (rv)
	{
	  ip6_full_reass_free (rm, rt, reass);
	  reass = NULL;
	  // if other worker created a context already work with the other copy
	  if (-2 == rv)
	    goto again;
	}
    }
  else
    {
      reass->key.as_u64[0] = ~0;
      reass->key.as_u64[1] = ~0;
      reass->key.as_u64[2] = ~0;
      reass->key.as_u64[3] = ~0;
      reass->key.as_u64[4] = ~0;
      reass->key.as_u64[5] = ~0;
    }

  return reass;
}

always_inline ip6_full_reass_rc_t
ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
			 ip6_full_reass_main_t * rm,
			 ip6_full_reass_per_thread_t * rt,
			 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
			 u32 * error0, bool is_custom_app)
{
  *bi0 = reass->first_bi;
  *error0 = IP6_ERROR_NONE;
  ip6_frag_hdr_t *frag_hdr;
  vlib_buffer_t *last_b = NULL;
  u32 sub_chain_bi = reass->first_bi;
  u32 total_length = 0;
  u32 buf_cnt = 0;
  u32 dropped_cnt = 0;
  u32 *vec_drop_compress = NULL;
  ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
  do
    {
      u32 tmp_bi = sub_chain_bi;
      vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
      vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
      if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
	  !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
	{
	  rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
	  goto free_buffers_and_return;
	}

      u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
      u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
	sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
      u32 trim_end =
	vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
      if (tmp_bi == reass->first_bi)
	{
	  /* first buffer - keep ip6 header */
	  if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
	    {
	      rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
	      goto free_buffers_and_return;
	    }
	  trim_front = 0;
	  trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
	    (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
	     sizeof (*frag_hdr));
	  if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
	    {
	      rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
	      goto free_buffers_and_return;
	    }
	}
      u32 keep_data =
	vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
      while (1)
	{
	  ++buf_cnt;
	  if (trim_front)
	    {
	      if (trim_front > tmp->current_length)
		{
		  /* drop whole buffer */
		  if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
		    {
		      rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
		      goto free_buffers_and_return;
		    }
		  trim_front -= tmp->current_length;
		  vec_add1 (vec_drop_compress, tmp_bi);
		  tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
		  tmp_bi = tmp->next_buffer;
		  tmp = vlib_get_buffer (vm, tmp_bi);
		  continue;
		}
	      else
		{
		  vlib_buffer_advance (tmp, trim_front);
		  trim_front = 0;
		}
	    }
	  if (keep_data)
	    {
	      if (last_b)
		{
		  last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
		  last_b->next_buffer = tmp_bi;
		}
	      last_b = tmp;
	      if (keep_data <= tmp->current_length)
		{
		  tmp->current_length = keep_data;
		  keep_data = 0;
		}
	      else
		{
		  keep_data -= tmp->current_length;
		  if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
		    {
		      rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
		      goto free_buffers_and_return;
		    }
		}
	      total_length += tmp->current_length;
	    }
	  else
	    {
	      if (reass->first_bi == tmp_bi)
		{
		  rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
		  goto free_buffers_and_return;
		}
	      vec_add1 (vec_drop_compress, tmp_bi);
	      ++dropped_cnt;
	    }
	  if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
	    {
	      tmp_bi = tmp->next_buffer;
	      tmp = vlib_get_buffer (vm, tmp->next_buffer);
	    }
	  else
	    {
	      break;
	    }
	}
      sub_chain_bi =
	vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
	reass.next_range_bi;
    }
  while (~0 != sub_chain_bi);

  if (!last_b)
    {
      rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
      goto free_buffers_and_return;
    }
  last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
  vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
  if (total_length < first_b->current_length)
    {
      rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
      goto free_buffers_and_return;
    }
  total_length -= first_b->current_length;
  first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
  first_b->total_length_not_including_first_buffer = total_length;
  // drop fragment header
  vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
  ip6_header_t *ip = vlib_buffer_get_current (first_b);
  u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
  ip6_ext_hdr_chain_t hdr_chain;
  ip6_ext_header_t *prev_hdr = 0;
  int res = ip6_ext_header_walk (first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
				 &hdr_chain);
  if (res < 0 ||
      (hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION))
    {
      rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
      goto free_buffers_and_return;
    }
  frag_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res].offset);
  if (res > 0)
    {
      prev_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res - 1].offset);
      prev_hdr->next_hdr = frag_hdr->next_hdr;
    }
  else
    {
      ip->protocol = frag_hdr->next_hdr;
    }
  if (hdr_chain.eh[res].offset != ip6_frag_hdr_offset)
    {
      rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
      goto free_buffers_and_return;
    }
  memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
	   first_b->current_length - ip6_frag_hdr_offset -
	   sizeof (ip6_frag_hdr_t));
  first_b->current_length -= sizeof (*frag_hdr);
  ip->payload_length =
    clib_host_to_net_u16 (total_length + first_b->current_length -
			  sizeof (*ip));
  if (!vlib_buffer_chain_linearize (vm, first_b))
    {
      rv = IP6_FULL_REASS_RC_NO_BUF;
      goto free_buffers_and_return;
    }
  first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
  if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
    {
      ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
				FINALIZE, ~0);
#if 0
      // following code does a hexdump of packet fragments to stdout ...
      do
	{
	  u32 bi = reass->first_bi;
	  u8 *s = NULL;
	  while (~0 != bi)
	    {
	      vlib_buffer_t *b = vlib_get_buffer (vm, bi);
	      s = format (s, "%u: %U\n", bi, format_hexdump,
			  vlib_buffer_get_current (b), b->current_length);
	      if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
		{
		  bi = b->next_buffer;
		}
	      else
		{
		  break;
		}
	    }
	  printf ("%.*s\n", vec_len (s), s);
	  fflush (stdout);
	  vec_free (s);
	}
      while (0);
#endif
    }
  if (!is_custom_app)
    {
      *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
    }
  else
    {
      *next0 = reass->next_index;
    }
  vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
  /* Keep track of number of successfully reassembled packets and number of
   * fragments reassembled */
  vlib_node_increment_counter (vm, node->node_index, IP6_ERROR_REASS_SUCCESS,
			       1);

  vlib_node_increment_counter (vm, node->node_index,
			       IP6_ERROR_REASS_FRAGMENTS_REASSEMBLED,
			       reass->fragments_n);

  ip6_full_reass_free (rm, rt, reass);
  reass = NULL;
free_buffers_and_return:
  vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
  vec_free (vec_drop_compress);
  return rv;
}

always_inline void
ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
				      ip6_full_reass_t * reass,
				      u32 prev_range_bi, u32 new_next_bi)
{

  vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
  vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
  if (~0 != prev_range_bi)
    {
      vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
      vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
      new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
      prev_vnb->ip.reass.next_range_bi = new_next_bi;
    }
  else
    {
      if (~0 != reass->first_bi)
	{
	  new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
	}
      reass->first_bi = new_next_bi;
    }
  reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
}

always_inline ip6_full_reass_rc_t
ip6_full_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
		       ip6_full_reass_main_t *rm,
		       ip6_full_reass_per_thread_t *rt,
		       ip6_full_reass_t *reass, u32 *bi0, u32 *next0,
		       u32 *error0, ip6_frag_hdr_t *frag_hdr,
		       bool is_custom_app, u32 *handoff_thread_idx,
		       int skip_bihash)
{
  int consumed = 0;
  vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
  vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
  if (is_custom_app)
    {
      reass->next_index = fvnb->ip.reass.next_index;	// store next_index before it's overwritten
      reass->error_next_index = fvnb->ip.reass.error_next_index;	// store error_next_index before it is overwritten
    }

  fvnb->ip.reass.ip6_frag_hdr_offset =
    (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
  ip6_header_t *fip = vlib_buffer_get_current (fb);
  if (fb->current_length < sizeof (*fip) ||
      fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
      fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
    {
      return IP6_FULL_REASS_RC_INTERNAL_ERROR;
    }

  u32 fragment_first = fvnb->ip.reass.fragment_first =
    ip6_frag_hdr_offset_bytes (frag_hdr);
  u32 fragment_length =
    vlib_buffer_length_in_chain (vm, fb) -
    (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
  if (0 == fragment_length)
    {
      return IP6_FULL_REASS_RC_INVALID_FRAG_LEN;
    }
  u32 fragment_last = fvnb->ip.reass.fragment_last =
    fragment_first + fragment_length - 1;
  int more_fragments = ip6_frag_hdr_more (frag_hdr);
  u32 candidate_range_bi = reass->first_bi;
  u32 prev_range_bi = ~0;
  fvnb->ip.reass.range_first = fragment_first;
  fvnb->ip.reass.range_last = fragment_last;
  fvnb->ip.reass.next_range_bi = ~0;
  if (!more_fragments)
    {
      reass->last_packet_octet = fragment_last;
    }
  if (~0 == reass->first_bi)
    {
      // starting a new reassembly
      ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
      reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
      consumed = 1;
      reass->fragments_n = 1;
      goto check_if_done_maybe;
    }
  reass->min_fragment_length =
    clib_min (clib_net_to_host_u16 (fip->payload_length),
	      fvnb->ip.reass.estimated_mtu);
  while (~0 != candidate_range_bi)
    {
      vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
      vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
      if (fragment_first > candidate_vnb->ip.reass.range_last)
	{
	  // this fragments starts after candidate range
	  prev_range_bi = candidate_range_bi;
	  candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
	  if (candidate_vnb->ip.reass.range_last < fragment_last &&
	      ~0 == candidate_range_bi)
	    {
	      // special case - this fragment falls beyond all known ranges
	      ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
						    *bi0);
	      consumed = 1;
	      break;
	    }
	  continue;
	}
      if (fragment_last < candidate_vnb->ip.reass.range_first)
	{
	  // this fragment ends before candidate range without any overlap
	  ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
						*bi0);
	  consumed = 1;
	}
      else if (fragment_first == candidate_vnb->ip.reass.range_first &&
	       fragment_last == candidate_vnb->ip.reass.range_last)
	{
	  // duplicate fragment - ignore
	}
      else
	{
	  // overlapping fragment - not allowed by RFC 8200
	  if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
	    {
	      ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr,
					RANGE_OVERLAP, ~0);
	    }
	  return IP6_FULL_REASS_RC_OVERLAP;
	}
      break;
    }
  ++reass->fragments_n;
check_if_done_maybe:
  if (consumed)
    {
      if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
	{
	  ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr, RANGE_NEW,
				    ~0);
	}
    }
  else if (skip_bihash)
    {
      // if this reassembly is not in bihash, then the packet must have been
      // consumed
      return IP6_FULL_REASS_RC_INTERNAL_ERROR;
    }
  if (~0 != reass->last_packet_octet &&
      reass->data_len == reass->last_packet_octet + 1)
    {
      *handoff_thread_idx = reass->sendout_thread_index;
      int handoff =
	reass->memory_owner_thread_index != reass->sendout_thread_index;
      ip6_full_reass_rc_t rc =
	ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
				 is_custom_app);
      if (IP6_FULL_REASS_RC_OK == rc && handoff)
	{
	  return IP6_FULL_REASS_RC_HANDOFF;
	}
      return rc;
    }
  else
    {
      if (skip_bihash)
	{
	  // if this reassembly is not in bihash, it should've been an atomic
	  // fragment and thus finalized
	  return IP6_FULL_REASS_RC_INTERNAL_ERROR;
	}
      if (consumed)
	{
	  *bi0 = ~0;
	  if (reass->fragments_n > rm->max_reass_len)
	    {
	      return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
	    }
	}
      else
	{
	  *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
	  *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
	}
    }
  return IP6_FULL_REASS_RC_OK;
}

always_inline bool
ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
					   vlib_buffer_t *b,
					   ip6_ext_hdr_chain_t *hc)
{
  int nh = hc->eh[hc->length - 1].protocol;
  /* Checking to see if it's a terminating header */
  if (ip6_ext_hdr (nh))
    {
      icmp6_error_set_vnet_buffer (
	b, ICMP6_parameter_problem,
	ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
      b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
      return false;
    }
  return true;
}

always_inline bool
ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t *vm,
					   vlib_node_runtime_t *node,
					   vlib_buffer_t *b,
					   ip6_frag_hdr_t *frag_hdr)
{
  vnet_buffer_opaque_t *vnb = vnet_buffer (b);
  ip6_header_t *ip = vlib_buffer_get_current (b);
  int more_fragments = ip6_frag_hdr_more (frag_hdr);
  u32 fragment_length =
    vlib_buffer_length_in_chain (vm, b) -
    (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
  if (more_fragments && 0 != fragment_length % 8)
    {
      icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
				   ICMP6_parameter_problem_erroneous_header_field,
				   (u8 *) & ip->payload_length - (u8 *) ip);
      b->error = node->errors[IP6_ERROR_REASS_INVALID_FRAG_SIZE];
      return false;
    }
  return true;
}

always_inline bool
ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t *vm,
					  vlib_node_runtime_t *node,
					  vlib_buffer_t *b,
					  ip6_frag_hdr_t *frag_hdr)
{
  vnet_buffer_opaque_t *vnb = vnet_buffer (b);
  u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
  u32 fragment_length =
    vlib_buffer_length_in_chain (vm, b) -
    (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
  if (fragment_first + fragment_length > 65535)
    {
      ip6_header_t *ip0 = vlib_buffer_get_current (b);
      icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
				   ICMP6_parameter_problem_erroneous_header_field,
				   (u8 *) & frag_hdr->fragment_offset_and_more
				   - (u8 *) ip0);
      b->error = node->errors[IP6_ERROR_REASS_INVALID_FRAG_SIZE];
      return false;
    }
  return true;
}

always_inline uword
ip6_full_reassembly_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
			    vlib_frame_t *frame, bool is_feature,
			    bool is_custom_app, bool is_local)
{
  u32 *from = vlib_frame_vector_args (frame);
  u32 n_left_from, n_left_to_next, *to_next, next_index;
  ip6_full_reass_main_t *rm = &ip6_full_reass_main;
  ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
  clib_spinlock_lock (&rt->lock);

  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;
  while (n_left_from > 0)
    {
      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
	  u32 error0 = IP6_ERROR_NONE;
	  u32 icmp_bi = ~0;

	  bi0 = from[0];
	  b0 = vlib_get_buffer (vm, bi0);

	  ip6_header_t *ip0 = vlib_buffer_get_current (b0);
	  ip6_frag_hdr_t *frag_hdr = NULL;
	  ip6_ext_hdr_chain_t hdr_chain;
	  vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);

	  int res = ip6_ext_header_walk (
	    b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
	  if (res < 0 ||
	      hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION)
	    {
	      vlib_node_increment_counter (vm, node->node_index,
					   IP6_ERROR_REASS_NO_FRAG_HDR, 1);
	      // this is a mangled packet - no fragmentation
	      next0 = is_custom_app ? fvnb->ip.reass.error_next_index :
					    IP6_FULL_REASSEMBLY_NEXT_DROP;
	      ip6_full_reass_add_trace (vm, node, NULL, bi0, NULL, PASSTHROUGH,
					~0);
	      goto skip_reass;
	    }
	  if (is_local && !rm->is_local_reass_enabled)
	    {
	      next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
	      goto skip_reass;
	    }

	  /* Keep track of received fragments */
	  vlib_node_increment_counter (vm, node->node_index,
				       IP6_ERROR_REASS_FRAGMENTS_RCVD, 1);
	  frag_hdr =
	    ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
	  vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
	    hdr_chain.eh[res].offset;

	  if (0 == ip6_frag_hdr_offset (frag_hdr))
	    {
	      // first fragment - verify upper-layer is present
	      if (!ip6_full_reass_verify_upper_layer_present (node, b0,
							      &hdr_chain))
		{
		  next0 = is_custom_app ? fvnb->ip.reass.error_next_index :
						IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
		  goto skip_reass;
		}
	    }

	  if (!ip6_full_reass_verify_fragment_multiple_8 (vm, node, b0,
							  frag_hdr) ||
	      !ip6_full_reass_verify_packet_size_lt_64k (vm, node, b0,
							 frag_hdr))
	    {
	      next0 = is_custom_app ? fvnb->ip.reass.error_next_index :
					    IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
	      goto skip_reass;
	    }

	  int skip_bihash = 0;
	  ip6_full_reass_kv_t kv;
	  u8 do_handoff = 0;

	  if (0 == ip6_frag_hdr_offset (frag_hdr) &&
	      !ip6_frag_hdr_more (frag_hdr))
	    {
	      // this is atomic fragment and needs to be processed separately
	      skip_bihash = 1;
	    }
	  else
	    {
	      kv.k.as_u64[0] = ip0->src_address.as_u64[0];
	      kv.k.as_u64[1] = ip0->src_address.as_u64[1];
	      kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
	      kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
	      kv.k.as_u64[4] =
		((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
				vnet_buffer (b0)->sw_if_index[VLIB_RX]))
		  << 32 |
		(u64) frag_hdr->identification;
	      /* RFC 8200: The Next Header values in the Fragment headers of
	       * different fragments of the same original packet may differ.
	       * Only the value from the Offset zero fragment packet is used
	       * for reassembly.
	       *
	       * Also, IPv6 Header doesnt contain the protocol value unlike
	       * IPv4.*/
	      kv.k.as_u64[5] = 0;
	    }

	  ip6_full_reass_t *reass = ip6_full_reass_find_or_create (
	    vm, node, rm, rt, &kv, &icmp_bi, &do_handoff, skip_bihash,
	    &n_left_to_next, &to_next);

	  if (reass)
	    {
	      const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
	      if (0 == fragment_first)
		{
		  reass->sendout_thread_index = vm->thread_index;
		}
	    }
	  if (PREDICT_FALSE (do_handoff))
	    {
	      next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
	      vnet_buffer (b0)->ip.reass.owner_thread_index =
		kv.v.memory_owner_thread_index;
	    }
	  else if (reass)
	    {
	      u32 handoff_thread_idx;
	      u32 counter = ~0;
	      switch (ip6_full_reass_update (
		vm, node, rm, rt, reass, &bi0, &next0, &error0, frag_hdr,
		is_custom_app, &handoff_thread_idx, skip_bihash))
		{
		case IP6_FULL_REASS_RC_OK:
		  /* nothing to do here */
		  break;
		case IP6_FULL_REASS_RC_HANDOFF:
		  next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
		  b0 = vlib_get_buffer (vm, bi0);
		  vnet_buffer (b0)->ip.reass.owner_thread_index =
		    handoff_thread_idx;
		  break;
		case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
		  counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
		  break;
		case IP6_FULL_REASS_RC_NO_BUF:
		  counter = IP6_ERROR_REASS_NO_BUF;
		  break;
		case IP6_FULL_REASS_RC_INVALID_FRAG_LEN:
		  counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
		  break;
		case IP6_FULL_REASS_RC_OVERLAP:
		  counter = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
		  break;
		case IP6_FULL_REASS_RC_INTERNAL_ERROR:
		  counter = IP6_ERROR_REASS_INTERNAL_ERROR;
		  /* Sanitization is needed in internal error cases only, as
		   * the incoming packet is already dropped in other cases,
		   * also adding bi0 back to the reassembly list, fixes the
		   * leaking of buffers during internal errors.
		   *
		   * Also it doesnt make sense to send these buffers custom
		   * app, these fragments are with internal errors */
		  sanitize_reass_buffers_add_missing (vm, reass, &bi0);
		  reass->error_next_index = ~0;
		  break;
		}
	      if (~0 != counter)
		{
		  vlib_node_increment_counter (vm, node->node_index, counter,
					       1);
		  ip6_full_reass_drop_all (vm, node, reass, &n_left_to_next,
					   &to_next);
		  ip6_full_reass_free (rm, rt, reass);
		  goto next_packet;
		  break;
		}
	    }
	  else
	    {
	      if (is_feature)
		{
		  next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
		}
	      else
		{
		  next0 = fvnb->ip.reass.error_next_index;
		}
	      error0 = IP6_ERROR_REASS_LIMIT_REACHED;
	    }

	  if (~0 != bi0)
	    {
	    skip_reass:
	      to_next[0] = bi0;
	      to_next += 1;
	      n_left_to_next -= 1;

	      /* bi0 might have been updated by reass_finalize, reload */
	      b0 = vlib_get_buffer (vm, bi0);
	      if (IP6_ERROR_NONE != error0)
		{
		  b0->error = node->errors[error0];
		}

	      if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
		{
		  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
		    {
		      ip6_full_reass_add_trace (
			vm, node, NULL, bi0, frag_hdr, HANDOFF,
			vnet_buffer (b0)->ip.reass.owner_thread_index);
		    }
		}
	      else if (is_feature && IP6_ERROR_NONE == error0)
		{
		  vnet_feature_next (&next0, b0);
		}

	      /* Increment the counter to-custom-app also as this fragment is
	       * also going to application */
	      if (is_custom_app)
		{
		  vlib_node_increment_counter (
		    vm, node->node_index, IP6_ERROR_REASS_TO_CUSTOM_APP, 1);
		}

	      vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					       n_left_to_next, bi0, next0);
	    }

	  if (~0 != icmp_bi)
	    {
	      next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
	      to_next[0] = icmp_bi;
	      to_next += 1;
	      n_left_to_next -= 1;
	      vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					       n_left_to_next, icmp_bi,
					       next0);
	    }
	next_packet:
	  from += 1;
	  n_left_from -= 1;
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  clib_spinlock_unlock (&rt->lock);
  return frame->n_vectors;
}

static char *ip6_full_reassembly_error_strings[] = {
#define _(sym, string) string,
  foreach_ip6_error
#undef _
};

VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
				    vlib_node_runtime_t * node,
				    vlib_frame_t * frame)
{
  return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
				     false /* is_custom_app */,
				     false /* is_local */);
}

VLIB_REGISTER_NODE (ip6_full_reass_node) = {
    .name = "ip6-full-reassembly",
    .vector_size = sizeof (u32),
    .format_trace = format_ip6_full_reass_trace,
    .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
    .error_strings = ip6_full_reassembly_error_strings,
    .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
    .next_nodes =
        {
                [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
                [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
                [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
                [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
        },
};

VLIB_NODE_FN (ip6_local_full_reass_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
				     false /* is_custom_app */,
				     true /* is_local */);
}

VLIB_REGISTER_NODE (ip6_local_full_reass_node) = {
    .name = "ip6-local-full-reassembly",
    .vector_size = sizeof (u32),
    .format_trace = format_ip6_full_reass_trace,
    .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
    .error_strings = ip6_full_reassembly_error_strings,
    .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
    .next_nodes =
        {
                [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
                [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
                [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
                [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-local-full-reassembly-handoff",
        },
};

VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
					    vlib_node_runtime_t * node,
					    vlib_frame_t * frame)
{
  return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */,
				     false /* is_custom_app */,
				     false /* is_local */);
}

VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
    .name = "ip6-full-reassembly-feature",
    .vector_size = sizeof (u32),
    .format_trace = format_ip6_full_reass_trace,
    .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
    .error_strings = ip6_full_reassembly_error_strings,
    .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
    .next_nodes =
        {
                [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
                [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
                [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
                [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
        },
};

VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
    .arc_name = "ip6-unicast",
    .node_name = "ip6-full-reassembly-feature",
    .runs_before = VNET_FEATURES ("ip6-lookup",
                                  "ipsec6-input-feature"),
    .runs_after = 0,
};

VLIB_NODE_FN (ip6_full_reass_node_custom)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
				     true /* is_custom_app */,
				     false /* is_local */);
}

VLIB_REGISTER_NODE (ip6_full_reass_node_custom) = {
    .name = "ip6-full-reassembly-custom",
    .vector_size = sizeof (u32),
    .format_trace = format_ip6_full_reass_trace,
    .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
    .error_strings = ip6_full_reassembly_error_strings,
    .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
    .next_nodes =
        {
                [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
                [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
                [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
                [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-custom-hoff",
        },
};

#ifndef CLIB_MARCH_VARIANT
static u32
ip6_full_reass_get_nbuckets ()
{
  ip6_full_reass_main_t *rm = &ip6_full_reass_main;
  u32 nbuckets;
  u8 i;

  /* need more mem with more workers */
  nbuckets = (u32) (rm->max_reass_n * (vlib_num_workers () + 1) /
		    IP6_FULL_REASS_HT_LOAD_FACTOR);

  for (i = 0; i < 31; i++)
    if ((1 << i) >= nbuckets)
      break;
  nbuckets = 1 << i;

  return nbuckets;
}
#endif /* CLIB_MARCH_VARIANT */

typedef enum
{
  IP6_EVENT_CONFIG_CHANGED = 1,
} ip6_full_reass_event_t;

#ifndef CLIB_MARCH_VARIANT
typedef struct
{
  int failure;
  clib_bihash_48_8_t *new_hash;
} ip6_rehash_cb_ctx;

static int
ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
{
  ip6_rehash_cb_ctx *ctx = _ctx;
  if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
    {
      ctx->failure = 1;
    }
  return (BIHASH_WALK_CONTINUE);
}

static void
ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
			   u32 max_reassembly_length,
			   u32 expire_walk_interval_ms)
{
  ip6_full_reass_main.timeout_ms = timeout_ms;
  ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
  ip6_full_reass_main.max_reass_n = max_reassemblies;
  ip6_full_reass_main.max_reass_len = max_reassembly_length;
  ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
}

vnet_api_error_t
ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
		    u32 max_reassembly_length, u32 expire_walk_interval_ms)
{
  u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
  ip6_full_reass_set_params (timeout_ms, max_reassemblies,
			     max_reassembly_length, expire_walk_interval_ms);
  vlib_process_signal_event (ip6_full_reass_main.vlib_main,
			     ip6_full_reass_main.ip6_full_reass_expire_node_idx,
			     IP6_EVENT_CONFIG_CHANGED, 0);
  u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
  if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
    {
      clib_bihash_48_8_t new_hash;
      clib_memset (&new_hash, 0, sizeof (new_hash));
      ip6_rehash_cb_ctx ctx;
      ctx.failure = 0;
      ctx.new_hash = &new_hash;
      clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
			     new_nbuckets * 1024);
      clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
					       ip6_rehash_cb, &ctx);
      if (ctx.failure)
	{
	  clib_bihash_free_48_8 (&new_hash);
	  return -1;
	}
      else
	{
	  clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
	  clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
			    sizeof (ip6_full_reass_main.hash));
	  clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
	}
    }
  return 0;
}

vnet_api_error_t
ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
		    u32 * max_reassembly_length,
		    u32 * expire_walk_interval_ms)
{
  *timeout_ms = ip6_full_reass_main.timeout_ms;
  *max_reassemblies = ip6_full_reass_main.max_reass_n;
  *max_reassembly_length = ip6_full_reass_main.max_reass_len;
  *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
  return 0;
}

static clib_error_t *
ip6_full_reass_init_function (vlib_main_t * vm)
{
  ip6_full_reass_main_t *rm = &ip6_full_reass_main;
  clib_error_t *error = 0;
  u32 nbuckets;
  vlib_node_t *node;

  rm->vlib_main = vm;

  vec_validate (rm->per_thread_data, vlib_num_workers ());
  ip6_full_reass_per_thread_t *rt;
  vec_foreach (rt, rm->per_thread_data)
  {
    clib_spinlock_init (&rt->lock);
    pool_alloc (rt->pool, rm->max_reass_n);
  }

  node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
  ASSERT (node);
  rm->ip6_full_reass_expire_node_idx = node->index;

  ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
			     IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
			     IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
			     IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);

  nbuckets = ip6_full_reass_get_nbuckets ();
  clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
			 nbuckets * 1024);

  node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
  ASSERT (node);
  rm->ip6_icmp_error_idx = node->index;

  if ((error = vlib_call_init_function (vm, ip_main_init)))
    return error;
  ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
			 ip6_local_full_reass_node.index);
  rm->is_local_reass_enabled = 1;

  rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
  rm->fq_local_index =
    vlib_frame_queue_main_init (ip6_local_full_reass_node.index, 0);
  rm->fq_feature_index =
    vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
  rm->fq_custom_index =
    vlib_frame_queue_main_init (ip6_full_reass_node_custom.index, 0);

  rm->feature_use_refcount_per_intf = NULL;
  return error;
}

VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
#endif /* CLIB_MARCH_VARIANT */

static uword
ip6_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
			     CLIB_UNUSED (vlib_frame_t *f))
{
  ip6_full_reass_main_t *rm = &ip6_full_reass_main;
  uword event_type, *event_data = 0;

  while (true)
    {
      vlib_process_wait_for_event_or_clock (vm,
					    (f64) rm->expire_walk_interval_ms
					    / (f64) MSEC_PER_SEC);
      event_type = vlib_process_get_events (vm, &event_data);

      switch (event_type)
	{
	case ~0:
	  /* no events => timeout */
	  /* fallthrough */
	case IP6_EVENT_CONFIG_CHANGED:
	  /* nothing to do here */
	  break;
	default:
	  clib_warning ("BUG: event type 0x%wx", event_type);
	  break;
	}
      f64 now = vlib_time_now (vm);

      ip6_full_reass_t *reass;
      int *pool_indexes_to_free = NULL;

      uword thread_index = 0;
      int index;
      const uword nthreads = vlib_num_workers () + 1;
      u32 *vec_icmp_bi = NULL;
      u32 n_left_to_next, *to_next;

      for (thread_index = 0; thread_index < nthreads; ++thread_index)
	{
	  ip6_full_reass_per_thread_t *rt =
	    &rm->per_thread_data[thread_index];
	  u32 reass_timeout_cnt = 0;
	  clib_spinlock_lock (&rt->lock);

	  vec_reset_length (pool_indexes_to_free);
	  /* Pace the number of timeouts handled per thread,to avoid barrier
	   * sync issues in real world scenarios */

	  u32 beg = rt->last_id;
	  /* to ensure we walk at least once per sec per context */
	  u32 end = beg + (IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT *
			     IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS /
			     MSEC_PER_SEC +
			   1);
	  if (end > vec_len (rt->pool))
	    {
	      end = vec_len (rt->pool);
	      rt->last_id = 0;
	    }
	  else
	    {
	      rt->last_id = end;
	    }

	  pool_foreach_stepping_index (index, beg, end, rt->pool)
	  {
	    reass = pool_elt_at_index (rt->pool, index);
	    if (now > reass->last_heard + rm->timeout)
	      {
		vec_add1 (pool_indexes_to_free, index);
	      }
	  }

	  int *i;
          vec_foreach (i, pool_indexes_to_free)
          {
            ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
            u32 icmp_bi = ~0;

	    reass_timeout_cnt += reass->fragments_n;
	    ip6_full_reass_on_timeout (vm, node, reass, &icmp_bi,
				       &n_left_to_next, &to_next);
	    if (~0 != icmp_bi)
	      vec_add1 (vec_icmp_bi, icmp_bi);

	    ip6_full_reass_free (rm, rt, reass);
	  }

	  clib_spinlock_unlock (&rt->lock);
	  if (reass_timeout_cnt)
	    vlib_node_increment_counter (vm, node->node_index,
					 IP6_ERROR_REASS_TIMEOUT,
					 reass_timeout_cnt);
	}

      while (vec_len (vec_icmp_bi) > 0)
	{
	  vlib_frame_t *f =
	    vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
	  u32 *to_next = vlib_frame_vector_args (f);
	  u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
	  int trace_frame = 0;
	  while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
	    {
	      u32 bi = vec_pop (vec_icmp_bi);
	      vlib_buffer_t *b = vlib_get_buffer (vm, bi);
	      if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
		trace_frame = 1;
	      to_next[0] = bi;
	      ++f->n_vectors;
	      to_next += 1;
	      n_left_to_next -= 1;
	    }
	  f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
	  vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
	}

      vec_free (pool_indexes_to_free);
      vec_free (vec_icmp_bi);
      if (event_data)
	{
	  vec_set_len (event_data, 0);
	}
    }

  return 0;
}

VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
    .function = ip6_full_reass_walk_expired,
    .format_trace = format_ip6_full_reass_trace,
    .type = VLIB_NODE_TYPE_PROCESS,
    .name = "ip6-full-reassembly-expire-walk",

    .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
    .error_strings = ip6_full_reassembly_error_strings,

};

static u8 *
format_ip6_full_reass_key (u8 * s, va_list * args)
{
  ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
  s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
	      key->xx_id, format_ip6_address, &key->src, format_ip6_address,
	      &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
  return s;
}

static u8 *
format_ip6_full_reass (u8 * s, va_list * args)
{
  vlib_main_t *vm = va_arg (*args, vlib_main_t *);
  ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);

  s = format (s, "ID: %lu, key: %U\n  first_bi: %u, data_len: %u, "
	      "last_packet_octet: %u, trace_op_counter: %u\n",
	      reass->id, format_ip6_full_reass_key, &reass->key,
	      reass->first_bi, reass->data_len, reass->last_packet_octet,
	      reass->trace_op_counter);
  u32 bi = reass->first_bi;
  u32 counter = 0;
  while (~0 != bi)
    {
      vlib_buffer_t *b = vlib_get_buffer (vm, bi);
      vnet_buffer_opaque_t *vnb = vnet_buffer (b);
      s = format (s, "  #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
		  "fragment[%u, %u]\n",
		  counter, vnb->ip.reass.range_first,
		  vnb->ip.reass.range_last, bi,
		  ip6_full_reass_buffer_get_data_offset (b),
		  ip6_full_reass_buffer_get_data_len (b),
		  vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
      if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
	{
	  bi = b->next_buffer;
	}
      else
	{
	  bi = ~0;
	}
    }
  return s;
}

static clib_error_t *
show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
		     CLIB_UNUSED (vlib_cli_command_t * lmd))
{
  ip6_full_reass_main_t *rm = &ip6_full_reass_main;

  vlib_cli_output (vm, "---------------------");
  vlib_cli_output (vm, "IP6 reassembly status");
  vlib_cli_output (vm, "---------------------");
  bool details = false;
  if (unformat (input, "details"))
    {
      details = true;
    }

  u32 sum_reass_n = 0;
  u64 sum_buffers_n = 0;
  ip6_full_reass_t *reass;
  uword thread_index;
  const uword nthreads = vlib_num_workers () + 1;
  for (thread_index = 0; thread_index < nthreads; ++thread_index)
    {
      ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
      clib_spinlock_lock (&rt->lock);
      if (details)
	{
          pool_foreach (reass, rt->pool) {
            vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
          }
	}
      sum_reass_n += rt->reass_n;
      clib_spinlock_unlock (&rt->lock);
    }
  vlib_cli_output (vm, "---------------------");
  vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
		   (long unsigned) sum_reass_n);
  vlib_cli_output (vm,
		   "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
		   (long unsigned) rm->max_reass_n);
  vlib_cli_output (vm,
		   "Maximum configured amount of fragments "
		   "per full IP6 reassembly: %lu\n",
		   (long unsigned) rm->max_reass_len);
  vlib_cli_output (vm,
		   "Maximum configured full IP6 reassembly timeout: %lums\n",
		   (long unsigned) rm->timeout_ms);
  vlib_cli_output (vm,
		   "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
		   (long unsigned) rm->expire_walk_interval_ms);
  vlib_cli_output (vm, "Buffers in use: %lu\n",
		   (long unsigned) sum_buffers_n);
  return 0;
}

VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
    .path = "show ip6-full-reassembly",
    .short_help = "show ip6-full-reassembly [details]",
    .function = show_ip6_full_reass,
};

#ifndef CLIB_MARCH_VARIANT
vnet_api_error_t
ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
{
  return vnet_feature_enable_disable ("ip6-unicast",
				      "ip6-full-reassembly-feature",
				      sw_if_index, enable_disable, 0, 0);
}
#endif /* CLIB_MARCH_VARIANT */

#define foreach_ip6_full_reassembly_handoff_error                       \
_(CONGESTION_DROP, "congestion drop")


typedef enum
{
#define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
  foreach_ip6_full_reassembly_handoff_error
#undef _
    IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
} ip6_full_reassembly_handoff_error_t;

static char *ip6_full_reassembly_handoff_error_strings[] = {
#define _(sym,string) string,
  foreach_ip6_full_reassembly_handoff_error
#undef _
};

typedef struct
{
  u32 next_worker_index;
} ip6_full_reassembly_handoff_trace_t;

static u8 *
format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  ip6_full_reassembly_handoff_trace_t *t =
    va_arg (*args, ip6_full_reassembly_handoff_trace_t *);

  s =
    format (s, "ip6-full-reassembly-handoff: next-worker %d",
	    t->next_worker_index);

  return s;
}

always_inline uword
ip6_full_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
				    vlib_frame_t *frame,
				    ip6_full_reass_node_type_t type,
				    bool is_local)
{
  ip6_full_reass_main_t *rm = &ip6_full_reass_main;

  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
  u32 n_enq, n_left_from, *from;
  u16 thread_indices[VLIB_FRAME_SIZE], *ti;
  u32 fq_index;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  vlib_get_buffers (vm, from, bufs, n_left_from);

  b = bufs;
  ti = thread_indices;

  switch (type)
    {
    case NORMAL:
      if (is_local)
	{
	  fq_index = rm->fq_local_index;
	}
      else
	{
	  fq_index = rm->fq_index;
	}
      break;
    case FEATURE:
      fq_index = rm->fq_feature_index;
      break;
    case CUSTOM:
      fq_index = rm->fq_custom_index;
      break;
    default:
      clib_warning ("Unexpected `type' (%d)!", type);
      ASSERT (0);
    }
  while (n_left_from > 0)
    {
      ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;

      if (PREDICT_FALSE
	  ((node->flags & VLIB_NODE_FLAG_TRACE)
	   && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
	{
	  ip6_full_reassembly_handoff_trace_t *t =
	    vlib_add_trace (vm, node, b[0], sizeof (*t));
	  t->next_worker_index = ti[0];
	}

      n_left_from -= 1;
      ti += 1;
      b += 1;
    }
  n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
					 thread_indices, frame->n_vectors, 1);

  if (n_enq < frame->n_vectors)
    vlib_node_increment_counter (vm, node->node_index,
				 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
				 frame->n_vectors - n_enq);
  return frame->n_vectors;
}

VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
						 vlib_node_runtime_t * node,
						 vlib_frame_t * frame)
{
  return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
					     false /* is_local */);
}

VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
  .name = "ip6-full-reassembly-handoff",
  .vector_size = sizeof (u32),
  .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
  .error_strings = ip6_full_reassembly_handoff_error_strings,
  .format_trace = format_ip6_full_reassembly_handoff_trace,

  .n_next_nodes = 1,

  .next_nodes = {
    [0] = "error-drop",
  },
};

VLIB_NODE_FN (ip6_local_full_reassembly_handoff_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
					     true /* is_feature */);
}

VLIB_REGISTER_NODE (ip6_local_full_reassembly_handoff_node) = {
  .name = "ip6-local-full-reassembly-handoff",
  .vector_size = sizeof (u32),
  .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
  .error_strings = ip6_full_reassembly_handoff_error_strings,
  .format_trace = format_ip6_full_reassembly_handoff_trace,

  .n_next_nodes = 1,

  .next_nodes = {
    [0] = "error-drop",
  },
};

VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
                               vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return ip6_full_reassembly_handoff_inline (vm, node, frame, FEATURE,
					     false /* is_local */);
}

VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
  .name = "ip6-full-reass-feature-hoff",
  .vector_size = sizeof (u32),
  .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
  .error_strings = ip6_full_reassembly_handoff_error_strings,
  .format_trace = format_ip6_full_reassembly_handoff_trace,

  .n_next_nodes = 1,

  .next_nodes = {
    [0] = "error-drop",
  },
};

VLIB_NODE_FN (ip6_full_reassembly_custom_handoff_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  return ip6_full_reassembly_handoff_inline (vm, node, frame, CUSTOM,
					     false /* is_local */);
}

VLIB_REGISTER_NODE (ip6_full_reassembly_custom_handoff_node) = {
  .name = "ip6-full-reass-custom-hoff",
  .vector_size = sizeof (u32),
  .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
  .error_strings = ip6_full_reassembly_handoff_error_strings,
  .format_trace = format_ip6_full_reassembly_handoff_trace,

  .n_next_nodes = 1,

  .next_nodes = {
    [0] = "error-drop",
  },
};

#ifndef CLIB_MARCH_VARIANT
int
ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
{
  ip6_full_reass_main_t *rm = &ip6_full_reass_main;
  vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
  if (is_enable)
    {
      if (!rm->feature_use_refcount_per_intf[sw_if_index])
	{
	  ++rm->feature_use_refcount_per_intf[sw_if_index];
	  return vnet_feature_enable_disable ("ip6-unicast",
					      "ip6-full-reassembly-feature",
					      sw_if_index, 1, 0, 0);
	}
      ++rm->feature_use_refcount_per_intf[sw_if_index];
    }
  else
    {
      --rm->feature_use_refcount_per_intf[sw_if_index];
      if (!rm->feature_use_refcount_per_intf[sw_if_index])
	return vnet_feature_enable_disable ("ip6-unicast",
					    "ip6-full-reassembly-feature",
					    sw_if_index, 0, 0, 0);
    }
  return -1;
}

void
ip6_local_full_reass_enable_disable (int enable)
{
  if (enable)
    {
      if (!ip6_full_reass_main.is_local_reass_enabled)
	{
	  ip6_full_reass_main.is_local_reass_enabled = 1;
	  ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
				 ip6_local_full_reass_node.index);
	}
    }
  else
    {
      if (ip6_full_reass_main.is_local_reass_enabled)
	{
	  ip6_full_reass_main.is_local_reass_enabled = 0;
	  ip6_unregister_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION);
	}
    }
}

int
ip6_local_full_reass_enabled ()
{
  return ip6_full_reass_main.is_local_reass_enabled;
}

#endif

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
">self.papi.nat64_add_del_interface_addr, {'is_add': is_add, 'sw_if_index': sw_if_index}) def dslite_set_aftr_addr(self, ip6, ip4): """Set DS-Lite AFTR addresses :param ip4: IPv4 address :param ip6: IPv6 address """ return self.api( self.papi.dslite_set_aftr_addr, {'ip4_addr': ip4, 'ip6_addr': ip6}) def dslite_set_b4_addr(self, ip6, ip4): """Set DS-Lite B4 IPv6 address :param ip4: IPv4 address :param ip6: IPv6 address """ return self.api( self.papi.dslite_set_b4_addr, {'ip4_addr': ip4, 'ip6_addr': ip6}) def dslite_add_del_pool_addr_range( self, start_addr, end_addr, is_add=1): """Add/del address range to DS-Lite pool :param start_addr: First IP address :param end_addr: Last IP address :param is_add: 1 if add, 0 if delete (Default value = 1) """ return self.api( self.papi.dslite_add_del_pool_addr_range, {'start_addr': start_addr, 'end_addr': end_addr, 'is_add': is_add}) def nat66_add_del_interface( self, sw_if_index, is_inside=1, is_add=1): """Enable/disable NAT66 feature on the interface :param sw_if_index: Index of the interface :param is_inside: 1 if inside, 0 if outside (Default value = 1) :param is_add: 1 if add, 0 if delete (Default value = 1) """ return self.api( self.papi.nat66_add_del_interface, {'sw_if_index': sw_if_index, 'is_inside': is_inside, 'is_add': is_add}) def nat66_add_del_static_mapping( self, in_ip, out_ip, vrf_id=0, is_add=1): """Add/delete NAT66 static mapping :param in_ip: Inside IPv6 address :param out_ip: Outside IPv6 address :param vrf_id: VRF ID (Default value = 0) :param is_add: 1 if add, 0 if delete (Default value = 1) """ return self.api( self.papi.nat66_add_del_static_mapping, {'local_ip_address': in_ip, 'external_ip_address': out_ip, 'vrf_id': vrf_id, 'is_add': is_add}) def nat66_interface_dump(self): """Dump interfaces with NAT66 feature :return: Dictionary of interfaces with NAT66 feature """ return self.api(self.papi.nat66_interface_dump, {}) def nat66_static_mapping_dump(self): """Dump NAT66 static mappings :return: Dictionary of NAT66 static mappings """ return self.api(self.papi.nat66_static_mapping_dump, {}) def control_ping(self): self.api(self.papi.control_ping) def bfd_udp_add(self, sw_if_index, desired_min_tx, required_min_rx, detect_mult, local_addr, peer_addr, is_ipv6=0, bfd_key_id=None, conf_key_id=None): if bfd_key_id is None: return self.api(self.papi.bfd_udp_add, { 'sw_if_index': sw_if_index, 'desired_min_tx': desired_min_tx, 'required_min_rx': required_min_rx, 'local_addr': local_addr, 'peer_addr': peer_addr, 'is_ipv6': is_ipv6, 'detect_mult': detect_mult, }) else: return self.api(self.papi.bfd_udp_add, { 'sw_if_index': sw_if_index, 'desired_min_tx': desired_min_tx, 'required_min_rx': required_min_rx, 'local_addr': local_addr, 'peer_addr': peer_addr, 'is_ipv6': is_ipv6, 'detect_mult': detect_mult, 'is_authenticated': 1, 'bfd_key_id': bfd_key_id, 'conf_key_id': conf_key_id, }) def bfd_udp_mod(self, sw_if_index, desired_min_tx, required_min_rx, detect_mult, local_addr, peer_addr, is_ipv6=0): return self.api(self.papi.bfd_udp_mod, { 'sw_if_index': sw_if_index, 'desired_min_tx': desired_min_tx, 'required_min_rx': required_min_rx, 'local_addr': local_addr, 'peer_addr': peer_addr, 'is_ipv6': is_ipv6, 'detect_mult': detect_mult, }) def bfd_udp_auth_activate(self, sw_if_index, local_addr, peer_addr, is_ipv6=0, bfd_key_id=None, conf_key_id=None, is_delayed=False): return self.api(self.papi.bfd_udp_auth_activate, { 'sw_if_index': sw_if_index, 'local_addr': local_addr, 'peer_addr': peer_addr, 'is_ipv6': is_ipv6, 'is_delayed': 1 if is_delayed else 0, 'bfd_key_id': bfd_key_id, 'conf_key_id': conf_key_id, }) def bfd_udp_auth_deactivate(self, sw_if_index, local_addr, peer_addr, is_ipv6=0, is_delayed=False): return self.api(self.papi.bfd_udp_auth_deactivate, { 'sw_if_index': sw_if_index, 'local_addr': local_addr, 'peer_addr': peer_addr, 'is_ipv6': is_ipv6, 'is_delayed': 1 if is_delayed else 0, }) def bfd_udp_del(self, sw_if_index, local_addr, peer_addr, is_ipv6=0): return self.api(self.papi.bfd_udp_del, { 'sw_if_index': sw_if_index, 'local_addr': local_addr, 'peer_addr': peer_addr, 'is_ipv6': is_ipv6, }) def bfd_udp_session_dump(self): return self.api(self.papi.bfd_udp_session_dump, {}) def bfd_udp_session_set_flags(self, admin_up_down, sw_if_index, local_addr, peer_addr, is_ipv6=0): return self.api(self.papi.bfd_udp_session_set_flags, { 'admin_up_down': admin_up_down, 'sw_if_index': sw_if_index, 'local_addr': local_addr, 'peer_addr': peer_addr, 'is_ipv6': is_ipv6, }) def want_bfd_events(self, enable_disable=1): return self.api(self.papi.want_bfd_events, { 'enable_disable': enable_disable, 'pid': os.getpid(), }) def bfd_auth_set_key(self, conf_key_id, auth_type, key): return self.api(self.papi.bfd_auth_set_key, { 'conf_key_id': conf_key_id, 'auth_type': auth_type, 'key': key, 'key_len': len(key), }) def bfd_auth_del_key(self, conf_key_id): return self.api(self.papi.bfd_auth_del_key, { 'conf_key_id': conf_key_id, }) def bfd_auth_keys_dump(self): return self.api(self.papi.bfd_auth_keys_dump, {}) def bfd_udp_set_echo_source(self, sw_if_index): return self.api(self.papi.bfd_udp_set_echo_source, {'sw_if_index': sw_if_index}) def bfd_udp_del_echo_source(self): return self.api(self.papi.bfd_udp_del_echo_source, {}) def classify_add_del_table( self, is_add, mask, match_n_vectors=1, table_index=0xFFFFFFFF, nbuckets=2, memory_size=2097152, skip_n_vectors=0, next_table_index=0xFFFFFFFF, miss_next_index=0xFFFFFFFF, current_data_flag=0, current_data_offset=0): """ :param is_add: :param mask: :param match_n_vectors: (Default value = 1) :param table_index: (Default value = 0xFFFFFFFF) :param nbuckets: (Default value = 2) :param memory_size: (Default value = 2097152) :param skip_n_vectors: (Default value = 0) :param next_table_index: (Default value = 0xFFFFFFFF) :param miss_next_index: (Default value = 0xFFFFFFFF) :param current_data_flag: (Default value = 0) :param current_data_offset: (Default value = 0) """ mask_len = ((len(mask) - 1) / 16 + 1) * 16 mask = mask + '\0' * (mask_len - len(mask)) return self.api( self.papi.classify_add_del_table, {'is_add': is_add, 'table_index': table_index, 'nbuckets': nbuckets, 'memory_size': memory_size, 'skip_n_vectors': skip_n_vectors, 'match_n_vectors': match_n_vectors, 'next_table_index': next_table_index, 'miss_next_index': miss_next_index, 'current_data_flag': current_data_flag, 'current_data_offset': current_data_offset, 'mask_len': mask_len, 'mask': mask}) def classify_add_del_session( self, is_add, table_index, match, opaque_index=0xFFFFFFFF, hit_next_index=0xFFFFFFFF, advance=0, action=0, metadata=0): """ :param is_add: :param table_index: :param match: :param opaque_index: (Default value = 0xFFFFFFFF) :param hit_next_index: (Default value = 0xFFFFFFFF) :param advance: (Default value = 0) :param action: (Default value = 0) :param metadata: (Default value = 0) """ match_len = ((len(match) - 1) / 16 + 1) * 16 match = match + '\0' * (match_len - len(match)) return self.api( self.papi.classify_add_del_session, {'is_add': is_add, 'table_index': table_index, 'hit_next_index': hit_next_index, 'opaque_index': opaque_index, 'advance': advance, 'action': action, 'metadata': metadata, 'match_len': match_len, 'match': match}) def input_acl_set_interface( self, is_add, sw_if_index, ip4_table_index=0xFFFFFFFF, ip6_table_index=0xFFFFFFFF, l2_table_index=0xFFFFFFFF): """ :param is_add: :param sw_if_index: :param ip4_table_index: (Default value = 0xFFFFFFFF) :param ip6_table_index: (Default value = 0xFFFFFFFF) :param l2_table_index: (Default value = 0xFFFFFFFF) """ return self.api( self.papi.input_acl_set_interface, {'sw_if_index': sw_if_index, 'ip4_table_index': ip4_table_index, 'ip6_table_index': ip6_table_index, 'l2_table_index': l2_table_index, 'is_add': is_add}) def output_acl_set_interface( self, is_add, sw_if_index, ip4_table_index=0xFFFFFFFF, ip6_table_index=0xFFFFFFFF, l2_table_index=0xFFFFFFFF): """ :param is_add: :param sw_if_index: :param ip4_table_index: (Default value = 0xFFFFFFFF) :param ip6_table_index: (Default value = 0xFFFFFFFF) :param l2_table_index: (Default value = 0xFFFFFFFF) """ return self.api( self.papi.output_acl_set_interface, {'sw_if_index': sw_if_index, 'ip4_table_index': ip4_table_index, 'ip6_table_index': ip6_table_index, 'l2_table_index': l2_table_index, 'is_add': is_add}) def set_ipfix_exporter( self, collector_address, src_address, path_mtu, template_interval, vrf_id=0, collector_port=4739, udp_checksum=0): return self.api( self.papi.set_ipfix_exporter, { 'collector_address': collector_address, 'collector_port': collector_port, 'src_address': src_address, 'vrf_id': vrf_id, 'path_mtu': path_mtu, 'template_interval': template_interval, 'udp_checksum': udp_checksum, }) def dhcp_proxy_config(self, dhcp_server, dhcp_src_address, rx_table_id=0, server_table_id=0, is_add=1, is_ipv6=0): return self.api( self.papi.dhcp_proxy_config, { 'rx_vrf_id': rx_table_id, 'server_vrf_id': server_table_id, 'is_ipv6': is_ipv6, 'is_add': is_add, 'dhcp_server': dhcp_server, 'dhcp_src_address': dhcp_src_address, }) def dhcp_proxy_set_vss(self, table_id, vss_type=255, vpn_ascii_id="", oui=0, vpn_index=0, is_add=1, is_ip6=0): return self.api( self.papi.dhcp_proxy_set_vss, { 'tbl_id': table_id, 'vss_type': vss_type, 'vpn_ascii_id': vpn_ascii_id, 'oui': oui, 'vpn_index': vpn_index, 'is_add': is_add, 'is_ipv6': is_ip6, }) def dhcp_client_dump(self): return self.api(self.papi.dhcp_client_dump, {}) def dhcp_client(self, sw_if_index, hostname, client_id='', is_add=1, set_broadcast_flag=1, want_dhcp_events=0): return self.api( self.papi.dhcp_client_config, { 'is_add': is_add, 'client': { 'sw_if_index': sw_if_index, 'hostname': hostname, 'id': client_id, 'want_dhcp_event': want_dhcp_events, 'set_broadcast_flag': set_broadcast_flag, 'pid': os.getpid()} }) def ip_mroute_add_del(self, src_address, grp_address, grp_address_length, e_flags, next_hop_afi, next_hop_sw_if_index, next_hop_address, i_flags, bier_imp=0, rpf_id=0, table_id=0, is_add=1, is_ipv6=0, is_local=0): """ IP Multicast Route add/del """ return self.api( self.papi.ip_mroute_add_del, {'next_hop_sw_if_index': next_hop_sw_if_index, 'entry_flags': e_flags, 'itf_flags': i_flags, 'table_id': table_id, 'rpf_id': rpf_id, 'is_add': is_add, 'is_ipv6': is_ipv6, 'is_local': is_local, 'bier_imp': bier_imp, 'next_hop_afi': next_hop_afi, 'grp_address_length': grp_address_length, 'grp_address': grp_address, 'src_address': src_address, 'nh_address': next_hop_address}) def mfib_signal_dump(self): return self.api(self.papi.mfib_signal_dump, {}) def ip_mfib_dump(self): return self.api(self.papi.ip_mfib_dump, {}) def ip6_mfib_dump(self): return self.api(self.papi.ip6_mfib_dump, {}) def lisp_enable_disable(self, is_enabled): return self.api( self.papi.lisp_enable_disable, { 'is_en': is_enabled, }) def lisp_locator_set(self, ls_name, is_add=1): return self.api( self.papi.lisp_add_del_locator_set, { 'is_add': is_add, 'locator_set_name': ls_name }) def lisp_locator_set_dump(self): return self.api(self.papi.lisp_locator_set_dump, {}) def lisp_locator(self, ls_name, sw_if_index, priority=1, weight=1, is_add=1): return self.api( self.papi.lisp_add_del_locator, { 'is_add': is_add, 'locator_set_name': ls_name, 'sw_if_index': sw_if_index, 'priority': priority, 'weight': weight }) def lisp_locator_dump(self, is_index_set, ls_name=None, ls_index=0): return self.api( self.papi.lisp_locator_dump, { 'is_index_set': is_index_set, 'ls_name': ls_name, 'ls_index': ls_index, }) def lisp_local_mapping(self, ls_name, eid_type, eid, prefix_len, vni=0, key_id=0, key="", is_add=1): return self.api( self.papi.lisp_add_del_local_eid, { 'locator_set_name': ls_name, 'is_add': is_add, 'eid_type': eid_type, 'eid': eid, 'prefix_len': prefix_len, 'vni': vni, 'key_id': key_id, 'key': key }) def lisp_eid_table_dump(self, eid_set=0, prefix_length=0, vni=0, eid_type=0, eid=None, filter_opt=0): return self.api( self.papi.lisp_eid_table_dump, { 'eid_set': eid_set, 'prefix_length': prefix_length, 'vni': vni, 'eid_type': eid_type, 'eid': eid, 'filter': filter_opt, }) def lisp_remote_mapping(self, eid_type, eid, eid_prefix_len=0, vni=0, rlocs=[], rlocs_num=0, is_src_dst=0, is_add=1): return self.api( self.papi.lisp_add_del_remote_mapping, { 'is_add': is_add, 'eid_type': eid_type, 'eid': eid, 'eid_len': eid_prefix_len, 'rloc_num': rlocs_num, 'rlocs': rlocs, 'vni': vni, 'is_src_dst': is_src_dst, }) def lisp_adjacency(self, leid, reid, leid_len, reid_len, eid_type, is_add=1, vni=0): return self.api( self.papi.lisp_add_del_adjacency, { 'is_add': is_add, 'vni': vni, 'eid_type': eid_type, 'leid': leid, 'reid': reid, 'leid_len': leid_len, 'reid_len': reid_len, }) def lisp_adjacencies_get(self, vni=0): return self.api( self.papi.lisp_adjacencies_get, { 'vni': vni }) def map_add_domain(self, ip6_prefix, ip6_prefix_len, ip6_src, ip6_src_prefix_len, ip4_prefix, ip4_prefix_len, ea_bits_len=0, psid_offset=0, psid_length=0, is_translation=0, is_rfc6052=0, mtu=1280): return self.api( self.papi.map_add_domain, { 'ip6_prefix': ip6_prefix, 'ip6_prefix_len': ip6_prefix_len, 'ip4_prefix': ip4_prefix, 'ip4_prefix_len': ip4_prefix_len, 'ip6_src': ip6_src, 'ip6_src_prefix_len': ip6_src_prefix_len, 'ea_bits_len': ea_bits_len, 'psid_offset': psid_offset, 'psid_length': psid_length, 'is_translation': is_translation, 'is_rfc6052': is_rfc6052, 'mtu': mtu }) def gtpu_add_del_tunnel( self, src_addr, dst_addr, is_add=1, is_ipv6=0, mcast_sw_if_index=0xFFFFFFFF, encap_vrf_id=0, decap_next_index=0xFFFFFFFF, teid=0): """ :param is_add: (Default value = 1) :param is_ipv6: (Default value = 0) :param src_addr: :param dst_addr: :param mcast_sw_if_index: (Default value = 0xFFFFFFFF) :param encap_vrf_id: (Default value = 0) :param decap_next_index: (Default value = 0xFFFFFFFF) :param teid: (Default value = 0) """ return self.api(self.papi.gtpu_add_del_tunnel, {'is_add': is_add, 'is_ipv6': is_ipv6, 'src_address': src_addr, 'dst_address': dst_addr, 'mcast_sw_if_index': mcast_sw_if_index, 'encap_vrf_id': encap_vrf_id, 'decap_next_index': decap_next_index, 'teid': teid}) def vxlan_gpe_add_del_tunnel( self, src_addr, dst_addr, mcast_sw_if_index=0xFFFFFFFF, is_add=1, is_ipv6=0, encap_vrf_id=0, decap_vrf_id=0, protocol=3, vni=0): """ :param local: :param remote: :param is_add: (Default value = 1) :param is_ipv6: (Default value = 0) :param encap_vrf_id: (Default value = 0) :param decap_vrf_id: (Default value = 0) :param mcast_sw_if_index: (Default value = 0xFFFFFFFF) :param protocol: (Default value = 3) :param vni: (Default value = 0) """ return self.api(self.papi.vxlan_gpe_add_del_tunnel, {'is_add': is_add, 'is_ipv6': is_ipv6, 'local': src_addr, 'remote': dst_addr, 'mcast_sw_if_index': mcast_sw_if_index, 'encap_vrf_id': encap_vrf_id, 'decap_vrf_id': decap_vrf_id, 'protocol': protocol, 'vni': vni}) def pppoe_add_del_session( self, client_ip, client_mac, session_id=0, is_add=1, is_ipv6=0, decap_vrf_id=0): """ :param is_add: (Default value = 1) :param is_ipv6: (Default value = 0) :param client_ip: :param session_id: (Default value = 0) :param client_mac: :param decap_vrf_id: (Default value = 0) """ return self.api(self.papi.pppoe_add_del_session, {'is_add': is_add, 'is_ipv6': is_ipv6, 'session_id': session_id, 'client_ip': client_ip, 'decap_vrf_id': decap_vrf_id, 'client_mac': client_mac}) def sr_mpls_policy_add(self, bsid, weight, type, segments): return self.api(self.papi.sr_mpls_policy_add, {'bsid': bsid, 'weight': weight, 'type': type, 'n_segments': len(segments), 'segments': segments}) def sr_mpls_policy_del(self, bsid): return self.api(self.papi.sr_mpls_policy_del, {'bsid': bsid}) def sr_localsid_add_del(self, localsid, behavior, nh_addr4, nh_addr6, is_del=0, end_psp=0, sw_if_index=0xFFFFFFFF, vlan_index=0, fib_table=0, ): """ Add/del IPv6 SR local-SID. :param localsid: :param behavior: END=1; END.X=2; END.DX2=4; END.DX6=5; :param behavior: END.DX4=6; END.DT6=7; END.DT4=8 :param nh_addr4: :param nh_addr6: :param is_del: (Default value = 0) :param end_psp: (Default value = 0) :param sw_if_index: (Default value = 0xFFFFFFFF) :param vlan_index: (Default value = 0) :param fib_table: (Default value = 0) """ return self.api( self.papi.sr_localsid_add_del, {'is_del': is_del, 'localsid': localsid, 'end_psp': end_psp, 'behavior': behavior, 'sw_if_index': sw_if_index, 'vlan_index': vlan_index, 'fib_table': fib_table, 'nh_addr4': nh_addr4, 'nh_addr6': nh_addr6 } ) def sr_policy_add( self, bsid_addr, weight=1, is_encap=1, type=0, fib_table=0, n_segments=0, segments=[]): """ :param bsid_addr: bindingSID of the SR Policy :param weight: weight of the sid list. optional. (default: 1) :param is_encap: (bool) whether SR policy should Encap or SRH insert \ (default: Encap) :param type: type/behavior of the SR policy. (default or spray) \ (default: default) :param fib_table: VRF where to install the FIB entry for the BSID \ (default: 0) :param n_segments: number of segments \ (default: 0) :param segments: a vector of IPv6 address composing the segment list \ (default: []) """ return self.api( self.papi.sr_policy_add, {'bsid_addr': bsid_addr, 'weight': weight, 'is_encap': is_encap, 'type': type, 'fib_table': fib_table, 'n_segments': n_segments, 'segments': segments } ) def sr_policy_del( self, bsid_addr, sr_policy_index=0): """ :param bsid: bindingSID of the SR Policy :param sr_policy_index: index of the sr policy (default: 0) """ return self.api( self.papi.sr_policy_del, {'bsid_addr': bsid_addr, 'sr_policy_index': sr_policy_index }) def sr_steering_add_del( self, is_del, bsid_addr, sr_policy_index, table_id, prefix_addr, mask_width, sw_if_index, traffic_type): """ Steer traffic L2 and L3 traffic through a given SR policy :param is_del: delete or add :param bsid_addr: bindingSID of the SR Policy (alt to sr_policy_index) :param sr_policy: is the index of the SR Policy (alt to bsid) :param table_id: is the VRF where to install the FIB entry for the BSID :param prefix_addr: is the IPv4/v6 address for L3 traffic type :param mask_width: is the mask for L3 traffic type :param sw_if_index: is the incoming interface for L2 traffic :param traffic_type: type of traffic (IPv4: 4, IPv6: 6, L2: 2) """ return self.api( self.papi.sr_steering_add_del, {'is_del': is_del, 'bsid_addr': bsid_addr, 'sr_policy_index': sr_policy_index, 'table_id': table_id, 'prefix_addr': prefix_addr, 'mask_width': mask_width, 'sw_if_index': sw_if_index, 'traffic_type': traffic_type }) def acl_add_replace(self, acl_index, r, tag='', expected_retval=0): """Add/replace an ACL :param int acl_index: ACL index to replace, 2^32-1 to create new ACL. :param acl_rule r: ACL rules array. :param str tag: symbolic tag (description) for this ACL. :param int count: number of rules. """ return self.api(self.papi.acl_add_replace, {'acl_index': acl_index, 'r': r, 'count': len(r), 'tag': tag}, expected_retval=expected_retval) def acl_del(self, acl_index, expected_retval=0): """ :param acl_index: :return: """ return self.api(self.papi.acl_del, {'acl_index': acl_index}, expected_retval=expected_retval) def acl_interface_set_acl_list(self, sw_if_index, n_input, acls, expected_retval=0): return self.api(self.papi.acl_interface_set_acl_list, {'sw_if_index': sw_if_index, 'count': len(acls), 'n_input': n_input, 'acls': acls}, expected_retval=expected_retval) def acl_interface_set_etype_whitelist(self, sw_if_index, n_input, whitelist, expected_retval=0): return self.api(self.papi.acl_interface_set_etype_whitelist, {'sw_if_index': sw_if_index, 'count': len(whitelist), 'n_input': n_input, 'whitelist': whitelist}, expected_retval=expected_retval) def acl_interface_add_del(self, sw_if_index, acl_index, is_add=1): """ Add/Delete ACL to/from interface :param sw_if_index: :param acl_index: :param is_add: (Default value = 1) """ return self.api(self.papi.acl_interface_add_del, {'is_add': is_add, 'is_input': 1, 'sw_if_index': sw_if_index, 'acl_index': acl_index}) def acl_dump(self, acl_index, expected_retval=0): return self.api(self.papi.acl_dump, {'acl_index': acl_index}, expected_retval=expected_retval) def acl_interface_list_dump(self, sw_if_index=0xFFFFFFFF, expected_retval=0): return self.api(self.papi.acl_interface_list_dump, {'sw_if_index': sw_if_index}, expected_retval=expected_retval) def macip_acl_add(self, rules, tag=""): """ Add MACIP acl :param rules: list of rules for given acl :param tag: acl tag """ return self.api(self.papi.macip_acl_add, {'r': rules, 'count': len(rules), 'tag': tag}) def macip_acl_add_replace(self, rules, acl_index=0xFFFFFFFF, tag=""): """ Add MACIP acl :param rules: list of rules for given acl :param tag: acl tag """ return self.api(self.papi.macip_acl_add_replace, {'acl_index': acl_index, 'r': rules, 'count': len(rules), 'tag': tag}) def macip_acl_del(self, acl_index): """ :param acl_index: :return: """ return self.api(self.papi.macip_acl_del, {'acl_index': acl_index}) def macip_acl_interface_add_del(self, sw_if_index, acl_index, is_add=1): """ Add MACIP acl to interface :param sw_if_index: :param acl_index: :param is_add: (Default value = 1) """ return self.api(self.papi.macip_acl_interface_add_del, {'is_add': is_add, 'sw_if_index': sw_if_index, 'acl_index': acl_index}) def macip_acl_interface_get(self): """ Return interface acls dump """ return self.api( self.papi.macip_acl_interface_get, {}) def macip_acl_dump(self, acl_index=4294967295): """ Return MACIP acl dump """ return self.api( self.papi.macip_acl_dump, {'acl_index': acl_index}) def policer_add_del(self, name, cir, eir, cb, eb, is_add=1, rate_type=0, round_type=0, ptype=0, color_aware=0, conform_action_type=1, conform_dscp=0, exceed_action_type=0, exceed_dscp=0, violate_action_type=0, violate_dscp=0): return self.api(self.papi.policer_add_del, {'name': name, 'cir': cir, 'eir': eir, 'cb': cb, 'eb': eb, 'is_add': is_add, 'rate_type': rate_type, 'round_type': round_type, 'type': ptype, 'color_aware': color_aware, 'conform_action_type': conform_action_type, 'conform_dscp': conform_dscp, 'exceed_action_type': exceed_action_type, 'exceed_dscp': exceed_dscp, 'violate_action_type': violate_action_type, 'violate_dscp': violate_dscp}) def ip_punt_police(self, policer_index, is_ip6=0, is_add=1): return self.api(self.papi.ip_punt_police, {'policer_index': policer_index, 'is_add': is_add, 'is_ip6': is_ip6}) def ip_punt_redirect(self, rx_sw_if_index, tx_sw_if_index, nh, is_ip6=0, is_add=1): return self.api(self.papi.ip_punt_redirect, {'rx_sw_if_index': rx_sw_if_index, 'tx_sw_if_index': tx_sw_if_index, 'nh': nh, 'is_add': is_add, 'is_ip6': is_ip6}) def bier_table_add_del(self, bti, mpls_label, is_add=1): """ BIER Table add/del """ return self.api( self.papi.bier_table_add_del, {'bt_tbl_id': {"bt_set": bti.set_id, "bt_sub_domain": bti.sub_domain_id, "bt_hdr_len_id": bti.hdr_len_id}, 'bt_label': mpls_label, 'bt_is_add': is_add}) def bier_table_dump(self): return self.api(self.papi.bier_table_dump, {}) def bier_route_add_del(self, bti, bp, paths, is_add=1): """ BIER Route add/del """ return self.api( self.papi.bier_route_add_del, {'br_tbl_id': {"bt_set": bti.set_id, "bt_sub_domain": bti.sub_domain_id, "bt_hdr_len_id": bti.hdr_len_id}, 'br_bp': bp, 'br_n_paths': len(paths), 'br_paths': paths, 'br_is_add': is_add}) def bier_route_dump(self, bti): return self.api( self.papi.bier_route_dump, {'br_tbl_id': {"bt_set": bti.set_id, "bt_sub_domain": bti.sub_domain_id, "bt_hdr_len_id": bti.hdr_len_id}}) def bier_imp_add(self, bti, src, ibytes, is_add=1): """ BIER Imposition Add """ return self.api( self.papi.bier_imp_add, {'bi_tbl_id': {"bt_set": bti.set_id, "bt_sub_domain": bti.sub_domain_id, "bt_hdr_len_id": bti.hdr_len_id}, 'bi_src': src, 'bi_n_bytes': len(ibytes), 'bi_bytes': ibytes}) def bier_imp_del(self, bi_index): """ BIER Imposition del """ return self.api( self.papi.bier_imp_del, {'bi_index': bi_index}) def bier_imp_dump(self): return self.api(self.papi.bier_imp_dump, {}) def bier_disp_table_add_del(self, bdti, is_add=1): """ BIER Disposition Table add/del """ return self.api( self.papi.bier_disp_table_add_del, {'bdt_tbl_id': bdti, 'bdt_is_add': is_add}) def bier_disp_table_dump(self): return self.api(self.papi.bier_disp_table_dump, {}) def bier_disp_entry_add_del(self, bdti, bp, payload_proto, next_hop_afi, next_hop, next_hop_tbl_id=0, next_hop_rpf_id=~0, next_hop_is_ip4=1, is_add=1): """ BIER Route add/del """ lstack = [] while (len(lstack) < 16): lstack.append({}) return self.api( self.papi.bier_disp_entry_add_del, {'bde_tbl_id': bdti, 'bde_bp': bp, 'bde_payload_proto': payload_proto, 'bde_n_paths': 1, 'bde_paths': [{'next_hop': next_hop, 'table_id': next_hop_tbl_id, 'afi': next_hop_afi, 'rpf_id': next_hop_rpf_id, 'n_labels': 0, 'label_stack': lstack}], 'bde_is_add': is_add}) def bier_disp_entry_dump(self, bdti): return self.api( self.papi.bier_disp_entry_dump, {'bde_tbl_id': bdti}) def add_node_next(self, node_name, next_name): """ Set the next node for a given node request :param node_name: :param next_name: """ return self.api(self.papi.add_node_next, {'node_name': node_name, 'next_name': next_name}) def session_enable_disable(self, is_enabled): return self.api( self.papi.session_enable_disable, {'is_enable': is_enabled}) def ipsec_spd_add_del(self, spd_id, is_add=1): """ SPD add/del - Wrapper to add or del ipsec SPD Sample CLI : 'ipsec spd add 1' :param spd_id - SPD ID to be created in the vpp . mandatory :param is_add - create (1) or delete(0) SPD (Default 1 - add) . optional :returns: reply from the API """ return self.api( self.papi.ipsec_spd_add_del, { 'spd_id': spd_id, 'is_add': is_add}) def ipsec_interface_add_del_spd(self, spd_id, sw_if_index, is_add=1): """ IPSEC interface SPD add/del - \ Wrapper to associate/disassociate SPD to interface in VPP Sample CLI : 'set interface ipsec spd GigabitEthernet0/6/0 1' :param spd_id - SPD ID to associate with the interface . mandatory :param sw_if_index - Interface Index which needs to ipsec \ association mandatory :param is_add - add(1) or del(0) association with interface \ (Default 1 - add) . optional :returns: reply from the API """ return self.api( self.papi.ipsec_interface_add_del_spd, {'spd_id': spd_id, 'sw_if_index': sw_if_index, 'is_add': is_add}) def ipsec_sad_add_del_entry(self, sad_id, spi, integrity_algorithm, integrity_key, crypto_algorithm, crypto_key, protocol, tunnel_src_address='', tunnel_dst_address='', is_tunnel=1, is_add=1, udp_encap=0): """ IPSEC SA add/del :param sad_id: security association ID :param spi: security param index of the SA in decimal :param integrity_algorithm: :param integrity_key: :param crypto_algorithm: :param crypto_key: :param protocol: AH(0) or ESP(1) protocol :param tunnel_src_address: tunnel mode outer src address :param tunnel_dst_address: tunnel mode outer dst address :param is_add: :param is_tunnel: :** reference /vpp/src/vnet/ipsec/ipsec.h file for enum values of crypto and ipsec algorithms """ return self.api( self.papi.ipsec_sad_add_del_entry, {'sad_id': sad_id, 'spi': spi, 'tunnel_src_address': tunnel_src_address, 'tunnel_dst_address': tunnel_dst_address, 'protocol': protocol, 'integrity_algorithm': integrity_algorithm, 'integrity_key_length': len(integrity_key), 'integrity_key': integrity_key, 'crypto_algorithm': crypto_algorithm, 'crypto_key_length': len(crypto_key) if crypto_key is not None else 0, 'crypto_key': crypto_key, 'is_add': is_add, 'is_tunnel': is_tunnel, 'udp_encap': udp_encap}) def ipsec_spd_add_del_entry(self, spd_id, sa_id, local_address_start, local_address_stop, remote_address_start, remote_address_stop, local_port_start=0, local_port_stop=65535, remote_port_start=0, remote_port_stop=65535, protocol=0, policy=0, priority=100, is_outbound=1, is_add=1, is_ip_any=0): """ IPSEC policy SPD add/del - Wrapper to configure ipsec SPD policy entries in VPP :param spd_id: SPD ID for the policy :param local_address_start: local-ip-range start address :param local_address_stop : local-ip-range stop address :param remote_address_start: remote-ip-range start address :param remote_address_stop : remote-ip-range stop address :param local_port_start: (Default value = 0) :param local_port_stop: (Default value = 65535) :param remote_port_start: (Default value = 0) :param remote_port_stop: (Default value = 65535) :param protocol: Any(0), AH(51) & ESP(50) protocol (Default value = 0) :param sa_id: Security Association ID for mapping it to SPD :param policy: bypass(0), discard(1), resolve(2) or protect(3) action (Default value = 0) :param priority: value for the spd action (Default value = 100) :param is_outbound: flag for inbound(0) or outbound(1) (Default value = 1) :param is_add: (Default value = 1) """ return self.api( self.papi.ipsec_spd_add_del_entry, {'spd_id': spd_id, 'sa_id': sa_id, 'local_address_start': local_address_start, 'local_address_stop': local_address_stop, 'remote_address_start': remote_address_start, 'remote_address_stop': remote_address_stop, 'local_port_start': local_port_start, 'local_port_stop': local_port_stop, 'remote_port_start': remote_port_start, 'remote_port_stop': remote_port_stop, 'is_add': is_add, 'protocol': protocol, 'policy': policy, 'priority': priority, 'is_outbound': is_outbound, 'is_ip_any': is_ip_any}) def ipsec_tunnel_if_add_del(self, local_ip, remote_ip, local_spi, remote_spi, crypto_alg, local_crypto_key, remote_crypto_key, integ_alg, local_integ_key, remote_integ_key, is_add=1, esn=0, anti_replay=1, renumber=0, show_instance=0): return self.api( self.papi.ipsec_tunnel_if_add_del, {'local_ip': local_ip, 'remote_ip': remote_ip, 'local_spi': local_spi, 'remote_spi': remote_spi, 'crypto_alg': crypto_alg, 'local_crypto_key_len': len(local_crypto_key), 'local_crypto_key': local_crypto_key, 'remote_crypto_key_len': len(remote_crypto_key), 'remote_crypto_key': remote_crypto_key, 'integ_alg': integ_alg, 'local_integ_key_len': len(local_integ_key), 'local_integ_key': local_integ_key, 'remote_integ_key_len': len(remote_integ_key), 'remote_integ_key': remote_integ_key, 'is_add': is_add, 'esn': esn, 'anti_replay': anti_replay, 'renumber': renumber, 'show_instance': show_instance }) def app_namespace_add(self, namespace_id, ip4_fib_id=0, ip6_fib_id=0, sw_if_index=0xFFFFFFFF, secret=0): return self.api( self.papi.app_namespace_add_del, {'secret': secret, 'sw_if_index': sw_if_index, 'ip4_fib_id': ip4_fib_id, 'ip6_fib_id': ip6_fib_id, 'namespace_id': namespace_id, 'namespace_id_len': len(namespace_id)}) def punt_socket_register(self, l4_port, pathname, header_version=1, is_ip4=1, l4_protocol=0x11): """ Punt to socket """ return self.api(self.papi.punt_socket_register, {'is_ip4': is_ip4, 'l4_protocol': l4_protocol, 'l4_port': l4_port, 'pathname': pathname, 'header_version': header_version}) def ip_reassembly_set(self, timeout_ms, max_reassemblies, expire_walk_interval_ms, is_ip6=0): """ Set IP reassembly parameters """ return self.api(self.papi.ip_reassembly_set, {'is_ip6': is_ip6, 'timeout_ms': timeout_ms, 'expire_walk_interval_ms': expire_walk_interval_ms, 'max_reassemblies': max_reassemblies}) def ip_reassembly_get(self, is_ip6=0): """ Get IP reassembly parameters """ return self.api(self.papi.ip_reassembly_get, {'is_ip6': is_ip6}) def ip_reassembly_enable_disable(self, sw_if_index, enable_ip4=False, enable_ip6=False): """ Enable/disable IP reassembly """ return self.api(self.papi.ip_reassembly_enable_disable, {'sw_if_index': sw_if_index, 'enable_ip4': 1 if enable_ip4 else 0, 'enable_ip6': 1 if enable_ip6 else 0, }) def gbp_endpoint_add_del(self, is_add, sw_if_index, addr, is_ip6, epg): """ GBP endpoint Add/Del """ return self.api(self.papi.gbp_endpoint_add_del, {'is_add': is_add, 'endpoint': { 'is_ip6': is_ip6, 'sw_if_index': sw_if_index, 'address': addr, 'epg_id': epg}}) def gbp_endpoint_dump(self): """ GBP endpoint Dump """ return self.api(self.papi.gbp_endpoint_dump, {}) def gbp_endpoint_group_add_del(self, is_add, epg, bd, ip4_rd, ip6_rd, uplink_sw_if_index): """ GBP endpoint group Add/Del """ return self.api(self.papi.gbp_endpoint_group_add_del, {'is_add': is_add, 'epg': { 'uplink_sw_if_index': uplink_sw_if_index, 'bd_id': bd, 'ip4_table_id': ip4_rd, 'ip6_table_id': ip6_rd, 'epg_id': epg}}) def gbp_endpoint_group_dump(self): """ GBP endpoint group Dump """ return self.api(self.papi.gbp_endpoint_group_dump, {}) def gbp_recirc_add_del(self, is_add, sw_if_index, epg, is_ext): """ GBP recirc Add/Del """ return self.api(self.papi.gbp_recirc_add_del, {'is_add': is_add, 'recirc': { 'is_ext': is_ext, 'sw_if_index': sw_if_index, 'epg_id': epg}}) def gbp_recirc_dump(self): """ GBP recirc Dump """ return self.api(self.papi.gbp_recirc_dump, {}) def gbp_subnet_add_del(self, is_add, table_id, is_internal, addr, addr_len, sw_if_index=0xffffffff, epg_id=0xffffffff, is_ip6=False): """ GBP Subnet Add/Del """ return self.api(self.papi.gbp_subnet_add_del, {'is_add': is_add, 'subnet': { 'is_internal': is_internal, 'is_ip6': is_ip6, 'sw_if_index': sw_if_index, 'epg_id': epg_id, 'address': addr, 'address_length': addr_len, 'table_id': table_id}}) def gbp_subnet_dump(self): """ GBP Subnet Dump """ return self.api(self.papi.gbp_subnet_dump, {}) def gbp_contract_add_del(self, is_add, src_epg, dst_epg, acl_index): """ GBP contract Add/Del """ return self.api(self.papi.gbp_contract_add_del, {'is_add': is_add, 'contract': { 'acl_index': acl_index, 'src_epg': src_epg, 'dst_epg': dst_epg}}) def gbp_contract_dump(self): """ GBP contract Dump """ return self.api(self.papi.gbp_contract_dump, {}) def ipip_6rd_add_tunnel(self, ip6_table_id, ip6_prefix, ip6_prefix_len, ip4_table_id, ip4_prefix, ip4_prefix_len, ip4_src, security_check): """ 6RD tunnel Add """ return self.api(self.papi.ipip_6rd_add_tunnel, {'ip4_table_id': ip4_table_id, 'ip6_table_id': ip6_table_id, 'ip6_prefix': ip6_prefix, 'ip6_prefix_len': ip6_prefix_len, 'ip4_prefix': ip4_prefix, 'ip4_prefix_len': ip4_prefix_len, 'ip4_src': ip4_src, 'security_check': security_check}) def ipip_6rd_del_tunnel(self, sw_if_index): """ 6RD tunnel Delete """ return self.api(self.papi.ipip_6rd_del_tunnel, {'sw_if_index': sw_if_index}) def ipip_add_tunnel(self, src_address, dst_address, is_ipv6=1, instance=0xFFFFFFFF, table_id=0, tc_tos=0): """ IPIP tunnel Add/Del """ return self.api(self.papi.ipip_add_tunnel, {'is_ipv6': is_ipv6, 'instance': instance, 'src_address': src_address, 'dst_address': dst_address, 'table_id': table_id, 'tc_tos': tc_tos}) def ipip_del_tunnel(self, sw_if_index): """ IPIP tunnel Delete """ return self.api(self.papi.ipip_del_tunnel, {'sw_if_index': sw_if_index}) def qos_egress_map_update(self, id, outputs): """ QOS egress map update """ return self.api(self.papi.qos_egress_map_update, {'map_id': id, 'rows': outputs}) def qos_egress_map_delete(self, id): """ QOS egress map delete """ return self.api(self.papi.qos_egress_map_delete, {'map_id': id}) def qos_mark_enable_disable(self, sw_if_index, output_source, map_id, enable): """ QOS Mark Enable/Disable """ return self.api(self.papi.qos_mark_enable_disable, {'map_id': map_id, 'sw_if_index': sw_if_index, 'output_source': output_source, 'enable': enable}) def qos_record_enable_disable(self, sw_if_index, input_source, enable): """ IP QoS recording Enble/Disable """ return self.api(self.papi.qos_record_enable_disable, {'sw_if_index': sw_if_index, 'input_source': input_source, 'enable': enable}) def igmp_enable_disable(self, sw_if_index, enable, host): """ Enable/disable IGMP on a given interface """ return self.api(self.papi.igmp_enable_disable, {'enable': enable, 'mode': host, 'sw_if_index': sw_if_index}) def igmp_listen(self, filter, sw_if_index, saddrs, gaddr): """ Listen for new (S,G) on specified interface :param enable: add/del :param sw_if_index: interface sw index :param saddr: source ip4 addr :param gaddr: group ip4 addr """ return self.api(self.papi.igmp_listen, { 'group': { 'filter': filter, 'sw_if_index': sw_if_index, 'n_srcs': len(saddrs), 'saddrs': saddrs, 'gaddr': { 'address': gaddr } } }) def igmp_dump(self, sw_if_index=None): """ Dump all (S,G) interface configurations """ if sw_if_index is None: sw_if_index = 0xffffffff return self.api(self.papi.igmp_dump, {'sw_if_index': sw_if_index}) def igmp_clear_interface(self, sw_if_index): """ Remove all (S,G)s from specified interface doesn't send IGMP report! """ return self.api( self.papi.igmp_clear_interface, { 'sw_if_index': sw_if_index}) def want_igmp_events(self, enable=1): return self.api(self.papi.want_igmp_events, {'enable': enable, 'pid': os.getpid()}) def bond_create( self, mode, lb, use_custom_mac, mac_address=''): """ :param mode: mode :param lb: load balance :param use_custom_mac: use custom mac :param mac_address: mac address """ return self.api( self.papi.bond_create, {'mode': mode, 'lb': lb, 'use_custom_mac': use_custom_mac, 'mac_address': mac_address }) def bond_delete( self, sw_if_index): """ :param sw_if_index: interface the operation is applied to """ return self.api(self.papi.bond_delete, {'sw_if_index': sw_if_index}) def bond_enslave( self, sw_if_index, bond_sw_if_index, is_passive, is_long_timeout): """ :param sw_if_index: slave sw_if_index :param bond_sw_if_index: bond sw_if_index :param is_passive: is passive lacp speaker :param is_long_time: 90 seconds timeout instead of 3 seconds timeout """ return self.api( self.papi.bond_enslave, {'sw_if_index': sw_if_index, 'bond_sw_if_index': bond_sw_if_index, 'is_passive': is_passive, 'is_long_timeout': is_long_timeout }) def bond_detach_slave( self, sw_if_index): """ :param sw_if_index: slave interface the operation is applied to """ return self.api(self.papi.bond_detach_slave, {'sw_if_index': sw_if_index}) def sw_interface_slave_dump( self, sw_if_index): """ :param sw_if_index: bond sw_if_index """ return self.api(self.papi.sw_interface_slave_dump, {'sw_if_index': sw_if_index}) def sw_interface_bond_dump( self): """ """ return self.api(self.papi.sw_interface_bond_dump, {}) def create_vhost_user_if( self, is_server, sock_filename, renumber, custom_dev_instance, use_custom_mac, mac_address, tag=''): """ :param is_server: is server :param sock_filename: socket name :param renumber: renumber :param custom_dev_instance: custom dev instance :param use_custom_mac: use custom mac :param mac_address: mac address :param tag: tag (default '' """ return self.api( self.papi.create_vhost_user_if, {'is_server': is_server, 'sock_filename': sock_filename, 'renumber': renumber, 'custom_dev_instance': custom_dev_instance, 'use_custom_mac': use_custom_mac, 'mac_address': mac_address, 'tag': tag }) def delete_vhost_user_if( self, sw_if_index): """ :param sw_if_index: interface the operation is applied to """ return self.api(self.papi.delete_vhost_user_if, {'sw_if_index': sw_if_index, }) def sw_interface_vhost_user_dump( self): """ """ return self.api(self.papi.sw_interface_vhost_user_dump, {}) def abf_policy_add_del(self, is_add, policy): return self.api( self.papi.abf_policy_add_del, {'is_add': is_add, 'policy': policy}) def abf_itf_attach_add_del(self, is_add, attach): return self.api( self.papi.abf_itf_attach_add_del, {'is_add': is_add, 'attach': attach}) def abf_policy_dump(self): return self.api( self.papi.abf_policy_dump, {}) def abf_itf_attach_dump(self): return self.api( self.papi.abf_itf_attach_dump, {}) def pipe_create(self, is_specified, user_instance): return self.api(self.papi.pipe_create, {'is_specified': is_specified, 'user_instance': user_instance}) def pipe_delete(self, parent_sw_if_index): return self.api(self.papi.pipe_delete, {'parent_sw_if_index': parent_sw_if_index}) def pipe_dump(self): return self.api(self.papi.pipe_dump, {})