summaryrefslogtreecommitdiffstats
path: root/zebra/interface.c
blob: 1afd9d5a7d8a92240af88142d6bc90fc92884df6 (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
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Interface function.
 * Copyright (C) 1997, 1999 Kunihiro Ishiguro
 */

#include <zebra.h>

#include "if.h"
#include "lib_errors.h"
#include "vty.h"
#include "sockunion.h"
#include "prefix.h"
#include "command.h"
#include "memory.h"
#include "ioctl.h"
#include "connected.h"
#include "log.h"
#include "zclient.h"
#include "vrf.h"
#include "lib/northbound_cli.h"

#include "zebra/rtadv.h"
#include "zebra_ns.h"
#include "zebra_vrf.h"
#include "zebra/interface.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/zebra_router.h"
#include "zebra/redistribute.h"
#include "zebra/debug.h"
#include "zebra/irdp.h"
#include "zebra/zebra_ptm.h"
#include "zebra/rt_netlink.h"
#include "zebra/if_netlink.h"
#include "zebra/interface.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_evpn_mh.h"

DEFINE_MTYPE_STATIC(ZEBRA, ZINFO, "Zebra Interface Information");

#define ZEBRA_PTM_SUPPORT

DEFINE_HOOK(zebra_if_extra_info, (struct vty * vty, struct interface *ifp),
	    (vty, ifp));
DEFINE_HOOK(zebra_if_config_wr, (struct vty * vty, struct interface *ifp),
	    (vty, ifp));

DEFINE_MTYPE(ZEBRA, ZIF_DESC, "Intf desc");

static void if_down_del_nbr_connected(struct interface *ifp);

static void if_zebra_speed_update(struct event *thread)
{
	struct interface *ifp = EVENT_ARG(thread);
	struct zebra_if *zif = ifp->info;
	uint32_t new_speed;
	bool changed = false;
	int error = 0;

	new_speed = kernel_get_speed(ifp, &error);

	/* error may indicate vrf not available or
	 * interfaces not available.
	 * note that loopback & virtual interfaces can return 0 as speed
	 */
	if (error < 0)
		return;

	if (new_speed != ifp->speed) {
		zlog_info("%s: %s old speed: %u new speed: %u", __func__,
			  ifp->name, ifp->speed, new_speed);
		ifp->speed = new_speed;
		if_add_update(ifp);
		changed = true;
	}

	if (changed || new_speed == UINT32_MAX) {
#define SPEED_UPDATE_SLEEP_TIME 5
#define SPEED_UPDATE_COUNT_MAX (4 * 60 / SPEED_UPDATE_SLEEP_TIME)
		/*
		 * Some interfaces never actually have an associated speed
		 * with them ( I am looking at you bridges ).
		 * So instead of iterating forever, let's give the
		 * system 4 minutes to try to figure out the speed
		 * if after that it it's probably never going to become
		 * useful.
		 * Since I don't know all the wonderful types of interfaces
		 * that may come into existence in the future I am going
		 * to not update the system to keep track of that.  This
		 * is far simpler to just stop trying after 4 minutes
		 */
		if (new_speed == UINT32_MAX &&
		    zif->speed_update_count == SPEED_UPDATE_COUNT_MAX)
			return;

		zif->speed_update_count++;
		event_add_timer(zrouter.master, if_zebra_speed_update, ifp,
				SPEED_UPDATE_SLEEP_TIME, &zif->speed_update);
		event_ignore_late_timer(zif->speed_update);
	}
}

static void zebra_if_node_destroy(route_table_delegate_t *delegate,
				  struct route_table *table,
				  struct route_node *node)
{
	if (node->info)
		list_delete((struct list **)&node->info);
	route_node_destroy(delegate, table, node);
}

static void zebra_if_nhg_dependents_free(struct zebra_if *zebra_if)
{
	nhg_connected_tree_free(&zebra_if->nhg_dependents);
}

static void zebra_if_nhg_dependents_init(struct zebra_if *zebra_if)
{
	nhg_connected_tree_init(&zebra_if->nhg_dependents);
}


route_table_delegate_t zebra_if_table_delegate = {
	.create_node = route_node_create,
	.destroy_node = zebra_if_node_destroy};

/* Called when new interface is added. */
static int if_zebra_new_hook(struct interface *ifp)
{
	struct zebra_if *zebra_if;

	zebra_if = XCALLOC(MTYPE_ZINFO, sizeof(struct zebra_if));
	zebra_if->ifp = ifp;

	zebra_if->multicast = IF_ZEBRA_DATA_UNSPEC;
	zebra_if->mpls_config = IF_ZEBRA_DATA_UNSPEC;
	zebra_if->shutdown = IF_ZEBRA_DATA_OFF;

	zebra_if->link_nsid = NS_UNKNOWN;

	zebra_if_nhg_dependents_init(zebra_if);

	zebra_ptm_if_init(zebra_if);

	ifp->ptm_enable = zebra_ptm_get_enable_state();

	rtadv_if_init(zebra_if);

	memset(&zebra_if->neigh_mac[0], 0, 6);

	/* Initialize installed address chains tree. */
	zebra_if->ipv4_subnets =
		route_table_init_with_delegate(&zebra_if_table_delegate);

	ifp->info = zebra_if;

	/*
	 * Some platforms are telling us that the interface is
	 * up and ready to go.  When we check the speed we
	 * sometimes get the wrong value.  Wait a couple
	 * of seconds and ask again.  Hopefully it's all settled
	 * down upon startup.
	 */
	zebra_if->speed_update_count = 0;
	event_add_timer(zrouter.master, if_zebra_speed_update, ifp, 15,
			&zebra_if->speed_update);
	event_ignore_late_timer(zebra_if->speed_update);

	return 0;
}

static void if_nhg_dependents_check_valid(struct nhg_hash_entry *nhe)
{
	zebra_nhg_check_valid(nhe);
}

static void if_down_nhg_dependents(const struct interface *ifp)
{
	struct nhg_connected *rb_node_dep = NULL;
	struct zebra_if *zif = (struct zebra_if *)ifp->info;

	frr_each(nhg_connected_tree, &zif->nhg_dependents, rb_node_dep)
		if_nhg_dependents_check_valid(rb_node_dep->nhe);
}

static void if_nhg_dependents_release(const struct interface *ifp)
{
	struct nhg_connected *rb_node_dep = NULL;
	struct zebra_if *zif = (struct zebra_if *)ifp->info;

	frr_each(nhg_connected_tree, &zif->nhg_dependents, rb_node_dep) {
		rb_node_dep->nhe->ifp = NULL; /* Null it out */
		if_nhg_dependents_check_valid(rb_node_dep->nhe);
	}
}

/* Called when interface is deleted. */
static int if_zebra_delete_hook(struct interface *ifp)
{
	struct zebra_if *zebra_if;
	struct zebra_l2info_bond *bond;

	if (ifp->info) {
		zebra_if = ifp->info;

		/* If we set protodown, clear our reason now from the kernel */
		if (ZEBRA_IF_IS_PROTODOWN(zebra_if) && zebra_if->protodown_rc &&
		    !ZEBRA_IF_IS_PROTODOWN_ONLY_EXTERNAL(zebra_if))
			zebra_if_update_protodown_rc(ifp, true,
						     (zebra_if->protodown_rc &
						      ~ZEBRA_PROTODOWN_ALL));

		/* Free installed address chains tree. */
		if (zebra_if->ipv4_subnets)
			route_table_finish(zebra_if->ipv4_subnets);

		rtadv_if_fini(zebra_if);

		bond = &zebra_if->bond_info;
		if (bond && bond->mbr_zifs)
			list_delete(&bond->mbr_zifs);

		zebra_l2_bridge_if_cleanup(ifp);
		zebra_evpn_if_cleanup(zebra_if);
		zebra_evpn_mac_ifp_del(ifp);

		if_nhg_dependents_release(ifp);
		zebra_if_nhg_dependents_free(zebra_if);

		XFREE(MTYPE_ZIF_DESC, zebra_if->desc);

		EVENT_OFF(zebra_if->speed_update);

		XFREE(MTYPE_ZINFO, zebra_if);
	}

	return 0;
}

/* Build the table key */
static void if_build_key(uint32_t ifindex, struct prefix *p)
{
	p->family = AF_INET;
	p->prefixlen = IPV4_MAX_BITLEN;
	p->u.prefix4.s_addr = ifindex;
}

/* Link an interface in a per NS interface tree */
struct interface *if_link_per_ns(struct zebra_ns *ns, struct interface *ifp)
{
	struct prefix p;
	struct route_node *rn;

	if (ifp->ifindex == IFINDEX_INTERNAL)
		return NULL;

	if_build_key(ifp->ifindex, &p);
	rn = route_node_get(ns->if_table, &p);
	if (rn->info) {
		ifp = (struct interface *)rn->info;
		route_unlock_node(rn); /* get */
		return ifp;
	}

	rn->info = ifp;
	ifp->node = rn;

	return ifp;
}

/* Delete a VRF. This is called in vrf_terminate(). */
void if_unlink_per_ns(struct interface *ifp)
{
	if (!ifp->node)
		return;

	ifp->node->info = NULL;
	route_unlock_node(ifp->node);
	ifp->node = NULL;
}

/* Look up an interface by identifier within a NS */
struct interface *if_lookup_by_index_per_ns(struct zebra_ns *ns,
					    uint32_t ifindex)
{
	struct prefix p;
	struct route_node *rn;
	struct interface *ifp = NULL;

	if_build_key(ifindex, &p);
	rn = route_node_lookup(ns->if_table, &p);
	if (rn) {
		ifp = (struct interface *)rn->info;
		route_unlock_node(rn); /* lookup */
	}
	return ifp;
}

/* Look up an interface by name within a NS */
struct interface *if_lookup_by_name_per_ns(struct zebra_ns *ns,
					   const char *ifname)
{
	struct route_node *rn;
	struct interface *ifp;

	for (rn = route_top(ns->if_table); rn; rn = route_next(rn)) {
		ifp = (struct interface *)rn->info;
		if (ifp && strcmp(ifp->name, ifname) == 0) {
			route_unlock_node(rn);
			return (ifp);
		}
	}

	return NULL;
}

struct interface *if_lookup_by_index_per_nsid(ns_id_t ns_id, uint32_t ifindex)
{
	struct zebra_ns *zns;

	zns = zebra_ns_lookup(ns_id);
	return zns ? if_lookup_by_index_per_ns(zns, ifindex) : NULL;
}

const char *ifindex2ifname_per_ns(struct zebra_ns *zns, unsigned int ifindex)
{
	struct interface *ifp;

	return ((ifp = if_lookup_by_index_per_ns(zns, ifindex)) != NULL)
		       ? ifp->name
		       : "unknown";
}

/* Tie an interface address to its derived subnet list of addresses. */
int if_subnet_add(struct interface *ifp, struct connected *ifc)
{
	struct route_node *rn;
	struct zebra_if *zebra_if;
	struct prefix cp;
	struct list *addr_list;

	assert(ifp && ifp->info && ifc);
	zebra_if = ifp->info;

	/* Get address derived subnet node and associated address list, while
	   marking
	   address secondary attribute appropriately. */
	cp = *CONNECTED_PREFIX(ifc);
	apply_mask(&cp);
	rn = route_node_get(zebra_if->ipv4_subnets, &cp);

	if ((addr_list = rn->info))
		SET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
	else {
		UNSET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
		rn->info = addr_list = list_new();
		route_lock_node(rn);
	}

	/* Tie address at the tail of address list. */
	listnode_add(addr_list, ifc);

	/* Return list element count. */
	return (addr_list->count);
}

/* Untie an interface address from its derived subnet list of addresses. */
int if_subnet_delete(struct interface *ifp, struct connected *ifc)
{
	struct route_node *rn;
	struct zebra_if *zebra_if;
	struct list *addr_list;
	struct prefix cp;

	assert(ifp && ifp->info && ifc);
	zebra_if = ifp->info;

	cp = *CONNECTED_PREFIX(ifc);
	apply_mask(&cp);

	/* Get address derived subnet node. */
	rn = route_node_lookup(zebra_if->ipv4_subnets, &cp);
	if (!(rn && rn->info)) {
		flog_warn(EC_ZEBRA_REMOVE_ADDR_UNKNOWN_SUBNET,
			  "Trying to remove an address from an unknown subnet. (please report this bug)");
		return -1;
	}
	route_unlock_node(rn);

	/* Untie address from subnet's address list. */
	addr_list = rn->info;

	/* Deleting an address that is not registered is a bug.
	 * In any case, we shouldn't decrement the lock counter if the address
	 * is unknown. */
	if (!listnode_lookup(addr_list, ifc)) {
		flog_warn(
			EC_ZEBRA_REMOVE_UNREGISTERED_ADDR,
			"Trying to remove an address from a subnet where it is not currently registered. (please report this bug)");
		return -1;
	}

	listnode_delete(addr_list, ifc);
	route_unlock_node(rn);

	/* Return list element count, if not empty. */
	if (addr_list->count) {
		/* If deleted address is primary, mark subsequent one as such
		 * and distribute. */
		if (!CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)) {
			ifc = listgetdata(
				(struct listnode *)listhead(addr_list));
			zebra_interface_address_delete_update(ifp, ifc);
			UNSET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
			/* XXX: Linux kernel removes all the secondary addresses
			 * when the primary
			 * address is removed. We could try to work around that,
			 * though this is
			 * non-trivial. */
			zebra_interface_address_add_update(ifp, ifc);
		}

		return addr_list->count;
	}

	/* Otherwise, free list and route node. */
	list_delete(&addr_list);
	rn->info = NULL;
	route_unlock_node(rn);

	return 0;
}

/* if_flags_mangle: A place for hacks that require mangling
 * or tweaking the interface flags.
 *
 * ******************** Solaris flags hacks **************************
 *
 * Solaris IFF_UP flag reflects only the primary interface as the
 * routing socket only sends IFINFO for the primary interface.  Hence
 * ~IFF_UP does not per se imply all the logical interfaces are also
 * down - which we only know of as addresses. Instead we must determine
 * whether the interface really is up or not according to how many
 * addresses are still attached. (Solaris always sends RTM_DELADDR if
 * an interface, logical or not, goes ~IFF_UP).
 *
 * Ie, we mangle IFF_UP to *additionally* reflect whether or not there
 * are addresses left in struct connected, not just the actual underlying
 * IFF_UP flag.
 *
 * We must hence remember the real state of IFF_UP, which we do in
 * struct zebra_if.primary_state.
 *
 * Setting IFF_UP within zebra to administratively shutdown the
 * interface will affect only the primary interface/address on Solaris.
 ************************End Solaris flags hacks ***********************
 */
static void if_flags_mangle(struct interface *ifp, uint64_t *newflags)
{
	return;
}

/* Update the flags field of the ifp with the new flag set provided.
 * Take whatever actions are required for any changes in flags we care
 * about.
 *
 * newflags should be the raw value, as obtained from the OS.
 */
void if_flags_update(struct interface *ifp, uint64_t newflags)
{
	if_flags_mangle(ifp, &newflags);

	if (if_is_no_ptm_operative(ifp)) {
		/* operative -> inoperative? */
		ifp->flags = newflags;
		if (!if_is_operative(ifp))
			if_down(ifp);
	} else {
		/* inoperative -> operative? */
		ifp->flags = newflags;
		if (if_is_operative(ifp))
			if_up(ifp, true);
	}
}

/* Wake up configured address if it is not in current kernel
   address. */
void if_addr_wakeup(struct interface *ifp)
{
	struct listnode *node, *nnode;
	struct connected *ifc;
	struct prefix *p;
	enum zebra_dplane_result dplane_res;

	for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, ifc)) {
		p = ifc->address;

		if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED)
		    && !CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)) {
			/* Address check. */
			if (p->family == AF_INET) {
				if (!if_is_up(ifp)) {
					/* Assume zebra is configured like
					 * following:
					 *
					 *   interface gre0
					 *    ip addr 192.0.2.1/24
					 *   !
					 *
					 * As soon as zebra becomes first aware
					 * that gre0 exists in the
					 * kernel, it will set gre0 up and
					 * configure its addresses.
					 *
					 * (This may happen at startup when the
					 * interface already exists
					 * or during runtime when the interface
					 * is added to the kernel)
					 *
					 * XXX: IRDP code is calling here via
					 * if_add_update - this seems
					 * somewhat weird.
					 * XXX: RUNNING is not a settable flag
					 * on any system
					 * I (paulj) am aware of.
					*/
					if_set_flags(ifp, IFF_UP | IFF_RUNNING);
					if_refresh(ifp);
				}

				dplane_res = dplane_intf_addr_set(ifp, ifc);
				if (dplane_res ==
				    ZEBRA_DPLANE_REQUEST_FAILURE) {
					flog_err_sys(
						EC_ZEBRA_IFACE_ADDR_ADD_FAILED,
						"Can't set interface's address: %s",
						dplane_res2str(dplane_res));
					continue;
				}

				SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
				/* The address will be advertised to zebra
				 * clients when the notification
				 * from the kernel has been received.
				 * It will also be added to the interface's
				 * subnet list then. */
			}
			if (p->family == AF_INET6) {
				if (!if_is_up(ifp)) {
					/* See long comment above */
					if_set_flags(ifp, IFF_UP | IFF_RUNNING);
					if_refresh(ifp);
				}


				dplane_res = dplane_intf_addr_set(ifp, ifc);
				if (dplane_res ==
				    ZEBRA_DPLANE_REQUEST_FAILURE) {
					flog_err_sys(
						EC_ZEBRA_IFACE_ADDR_ADD_FAILED,
						"Can't set interface's address: %s",
						dplane_res2str(dplane_res));
					continue;
				}

				SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
				/* The address will be advertised to zebra
				 * clients when the notification
				 * from the kernel has been received. */
			}
		}
	}
}

/* Handle interface addition */
void if_add_update(struct interface *ifp)
{
	struct zebra_if *if_data;
	struct zebra_ns *zns;
	struct zebra_vrf *zvrf = ifp->vrf->info;

	/* case interface populate before vrf enabled */
	if (zvrf->zns)
		zns = zvrf->zns;
	else
		zns = zebra_ns_lookup(NS_DEFAULT);
	if_link_per_ns(zns, ifp);
	if_data = ifp->info;
	assert(if_data);

	if (if_data->multicast == IF_ZEBRA_DATA_ON)
		if_set_flags(ifp, IFF_MULTICAST);
	else if (if_data->multicast == IF_ZEBRA_DATA_OFF)
		if_unset_flags(ifp, IFF_MULTICAST);

	zebra_ptm_if_set_ptm_state(ifp, if_data);

	zebra_interface_add_update(ifp);

	if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		SET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);

		if (if_data->shutdown == IF_ZEBRA_DATA_ON) {
			if (IS_ZEBRA_DEBUG_KERNEL) {
				zlog_debug(
					"interface %s vrf %s(%u) index %d is shutdown. Won't wake it up.",
					ifp->name, ifp->vrf->name,
					ifp->vrf->vrf_id, ifp->ifindex);
			}

			return;
		}

		if_addr_wakeup(ifp);

		if (if_data->mpls_config == IF_ZEBRA_DATA_ON)
			dplane_intf_mpls_modify_state(ifp, true);
		else if (if_data->mpls_config == IF_ZEBRA_DATA_OFF)
			dplane_intf_mpls_modify_state(ifp, false);

		if (IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug(
				"interface %s vrf %s(%u) index %d becomes active.",
				ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
				ifp->ifindex);

	} else {
		if (IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug("interface %s vrf %s(%u) index %d is added.",
				   ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
				   ifp->ifindex);
	}
}

/* Install connected routes corresponding to an interface. */
static void if_install_connected(struct interface *ifp)
{
	struct listnode *node;
	struct listnode *next;
	struct connected *ifc;

	if (ifp->connected) {
		for (ALL_LIST_ELEMENTS(ifp->connected, node, next, ifc)) {
			if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
				zebra_interface_address_add_update(ifp, ifc);

			connected_up(ifp, ifc);
		}
	}
}

/* Uninstall connected routes corresponding to an interface. */
static void if_uninstall_connected(struct interface *ifp)
{
	struct listnode *node;
	struct listnode *next;
	struct connected *ifc;

	if (ifp->connected) {
		for (ALL_LIST_ELEMENTS(ifp->connected, node, next, ifc)) {
			zebra_interface_address_delete_update(ifp, ifc);
			connected_down(ifp, ifc);
		}
	}
}

/* Uninstall and delete connected routes corresponding to an interface. */
/* TODO - Check why IPv4 handling here is different from install or if_down */
static void if_delete_connected(struct interface *ifp)
{
	struct connected *ifc;
	struct prefix cp;
	struct route_node *rn;
	struct zebra_if *zebra_if;
	struct listnode *node;
	struct listnode *last = NULL;

	zebra_if = ifp->info;

	if (!ifp->connected)
		return;

	while ((node = (last ? last->next : listhead(ifp->connected)))) {
		ifc = listgetdata(node);

		cp = *CONNECTED_PREFIX(ifc);
		apply_mask(&cp);

		if (cp.family == AF_INET
		    && (rn = route_node_lookup(zebra_if->ipv4_subnets, &cp))) {
			struct listnode *anode;
			struct listnode *next;
			struct listnode *first;
			struct list *addr_list;

			route_unlock_node(rn);
			addr_list = (struct list *)rn->info;

			/* Remove addresses, secondaries first. */
			first = listhead(addr_list);
			if (first)
				for (anode = first->next; anode || first;
				     anode = next) {
					if (!anode) {
						anode = first;
						first = NULL;
					}
					next = anode->next;

					ifc = listgetdata(anode);
					connected_down(ifp, ifc);

					/* XXX: We have to send notifications
					 * here explicitly, because we destroy
					 * the ifc before receiving the
					 * notification about the address being
					 * deleted.
					 */
					zebra_interface_address_delete_update(
						ifp, ifc);

					UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
					UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);

					/* Remove from subnet chain. */
					list_delete_node(addr_list, anode);
					route_unlock_node(rn);

					/* Remove from interface address list
					 * (unconditionally). */
					if (!CHECK_FLAG(ifc->conf,
							ZEBRA_IFC_CONFIGURED)) {
						listnode_delete(ifp->connected,
								ifc);
						connected_free(&ifc);
					} else
						last = node;
				}

			/* Free chain list and respective route node. */
			list_delete(&addr_list);
			rn->info = NULL;
			route_unlock_node(rn);
		} else if (cp.family == AF_INET6) {
			connected_down(ifp, ifc);

			zebra_interface_address_delete_update(ifp, ifc);

			UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
			UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);

			if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
				last = node;
			else {
				listnode_delete(ifp->connected, ifc);
				connected_free(&ifc);
			}
		} else {
			last = node;
		}
	}
}

/* Handle an interface delete event */
void if_delete_update(struct interface **pifp)
{
	struct zebra_if *zif;
	struct interface *ifp = *pifp;

	if (if_is_up(ifp)) {
		flog_err(
			EC_LIB_INTERFACE,
			"interface %s vrf %s(%u) index %d is still up while being deleted.",
			ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
			ifp->ifindex);
		return;
	}

	if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
		return;

	/* Mark interface as inactive */
	UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);

	if (IS_ZEBRA_DEBUG_KERNEL)
		zlog_debug("interface %s vrf %s(%u) index %d is now inactive.",
			   ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
			   ifp->ifindex);

	/* Delete connected routes from the kernel. */
	if_delete_connected(ifp);

	/* if the ifp is in a vrf, move it to default so vrf can be deleted if
	 * desired. This operation is not done for netns implementation to avoid
	 * collision with interface with the same name in the default vrf (can
	 * occur with this implementation whereas it is not possible with
	 * vrf-lite).
	 */
	if (ifp->vrf->vrf_id && !vrf_is_backend_netns())
		if_handle_vrf_change(ifp, VRF_DEFAULT);

	/* Send out notification on interface delete. */
	zebra_interface_delete_update(ifp);

	if_unlink_per_ns(ifp);

	/* Update ifindex after distributing the delete message.  This is in
	   case any client needs to have the old value of ifindex available
	   while processing the deletion.  Each client daemon is responsible
	   for setting ifindex to IFINDEX_INTERNAL after processing the
	   interface deletion message. */
	if_set_index(ifp, IFINDEX_INTERNAL);
	ifp->node = NULL;

	UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);

	/* Reset some zebra interface params to default values. */
	zif = ifp->info;
	if (zif) {
		zebra_evpn_if_cleanup(zif);
		zif->zif_type = ZEBRA_IF_OTHER;
		zif->zif_slave_type = ZEBRA_IF_SLAVE_NONE;
		memset(&zif->l2info, 0, sizeof(union zebra_l2if_info));
		memset(&zif->brslave_info, 0,
		       sizeof(struct zebra_l2info_brslave));
		zebra_evpn_mac_ifp_del(ifp);
	}

	if (!ifp->configured) {
		if (IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug("interface %s is being deleted from the system",
				   ifp->name);
		if_delete(pifp);
	}
}

/* VRF change for an interface */
void if_handle_vrf_change(struct interface *ifp, vrf_id_t vrf_id)
{
	vrf_id_t old_vrf_id;

	old_vrf_id = ifp->vrf->vrf_id;

	/* Uninstall connected routes. */
	if_uninstall_connected(ifp);

	/* Delete any IPv4 neighbors created to implement RFC 5549 */
	if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);

	/* Delete all neighbor addresses learnt through IPv6 RA */
	if_down_del_nbr_connected(ifp);

	/* Send out notification on interface VRF change. */
	/* This is to issue a DELETE, as appropriate. */
	zebra_interface_vrf_update_del(ifp, vrf_id);

	if (if_is_vrf(ifp))
		return;

	/* update VRF */
	if_update_to_new_vrf(ifp, vrf_id);

	/* Send out notification on interface VRF change. */
	/* This is to issue an ADD, if needed. */
	zebra_interface_vrf_update_add(ifp, old_vrf_id);
}

static void ipv6_ll_address_to_mac(struct in6_addr *address, uint8_t *mac)
{
	mac[0] = address->s6_addr[8] ^ 0x02;
	mac[1] = address->s6_addr[9];
	mac[2] = address->s6_addr[10];
	mac[3] = address->s6_addr[13];
	mac[4] = address->s6_addr[14];
	mac[5] = address->s6_addr[15];
}

void if_nbr_mac_to_ipv4ll_neigh_update(struct interface *ifp,
				       char mac[6],
				       struct in6_addr *address,
				       int add)
{
	struct zebra_vrf *zvrf = ifp->vrf->info;
	struct zebra_if *zif = ifp->info;
	char buf[16] = "169.254.0.1";
	struct in_addr ipv4_ll;
	ns_id_t ns_id;

	inet_pton(AF_INET, buf, &ipv4_ll);

	ns_id = zvrf->zns->ns_id;

	/*
	 * Remove and re-add any existing neighbor entry for this address,
	 * since Netlink doesn't currently offer update message types.
	 */
	kernel_neigh_update(0, ifp->ifindex, (void *)&ipv4_ll.s_addr, mac, 6,
			    ns_id, AF_INET, true);

	/* Add new neighbor entry.
	 *
	 * We force installation even if current neighbor entry is the same.
	 * Since this function is used to refresh our MAC entries after an
	 * interface flap, if we don't force in our custom entries with their
	 * state set to PERMANENT or REACHABLE then the kernel will attempt to
	 * resolve our leftover entries, fail, mark them unreachable and then
	 * they'll be useless to us.
	 */
	if (add)
		kernel_neigh_update(add, ifp->ifindex, (void *)&ipv4_ll.s_addr,
				    mac, 6, ns_id, AF_INET, true);

	memcpy(&zif->neigh_mac[0], &mac[0], 6);

	/*
	 * We need to note whether or not we originated a v6
	 * neighbor entry for this interface.  So that when
	 * someone unwisely accidentally deletes this entry
	 * we can shove it back in.
	 */
	zif->v6_2_v4_ll_neigh_entry = !!add;
	memcpy(&zif->v6_2_v4_ll_addr6, address, sizeof(*address));

	zvrf->neigh_updates++;
}

void if_nbr_ipv6ll_to_ipv4ll_neigh_update(struct interface *ifp,
					  struct in6_addr *address, int add)
{

	char mac[6];

	ipv6_ll_address_to_mac(address, (uint8_t *)mac);
	if_nbr_mac_to_ipv4ll_neigh_update(ifp, mac, address, add);
}

static void if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(struct interface *ifp)
{
	if (listhead(ifp->nbr_connected)) {
		struct nbr_connected *nbr_connected;
		struct listnode *node;

		for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
					  nbr_connected))
			if_nbr_ipv6ll_to_ipv4ll_neigh_update(
				ifp, &nbr_connected->address->u.prefix6, 1);
	}
}

void if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(struct interface *ifp)
{
	if (listhead(ifp->nbr_connected)) {
		struct nbr_connected *nbr_connected;
		struct listnode *node;

		for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
					  nbr_connected))
			if_nbr_ipv6ll_to_ipv4ll_neigh_update(
				ifp, &nbr_connected->address->u.prefix6, 0);
	}
}

static void if_down_del_nbr_connected(struct interface *ifp)
{
	struct nbr_connected *nbr_connected;
	struct listnode *node, *nnode;

	for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode,
			       nbr_connected)) {
		listnode_delete(ifp->nbr_connected, nbr_connected);
		nbr_connected_free(nbr_connected);
	}
}

void if_nhg_dependents_add(struct interface *ifp, struct nhg_hash_entry *nhe)
{
	if (ifp->info) {
		struct zebra_if *zif = (struct zebra_if *)ifp->info;

		nhg_connected_tree_add_nhe(&zif->nhg_dependents, nhe);
	}
}

void if_nhg_dependents_del(struct interface *ifp, struct nhg_hash_entry *nhe)
{
	if (ifp->info) {
		struct zebra_if *zif = (struct zebra_if *)ifp->info;

		nhg_connected_tree_del_nhe(&zif->nhg_dependents, nhe);
	}
}

unsigned int if_nhg_dependents_count(const struct interface *ifp)
{
	if (ifp->info) {
		struct zebra_if *zif = (struct zebra_if *)ifp->info;

		return nhg_connected_tree_count(&zif->nhg_dependents);
	}

	return 0;
}


bool if_nhg_dependents_is_empty(const struct interface *ifp)
{
	if (ifp->info) {
		struct zebra_if *zif = (struct zebra_if *)ifp->info;

		return nhg_connected_tree_is_empty(&zif->nhg_dependents);
	}

	return false;
}

/* Interface is up. */
void if_up(struct interface *ifp, bool install_connected)
{
	struct zebra_if *zif;
	struct interface *link_if;

	zif = ifp->info;
	zif->up_count++;
	frr_timestamp(2, zif->up_last, sizeof(zif->up_last));

	/* Notify the protocol daemons. */
	if (ifp->ptm_enable && (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)) {
		flog_warn(EC_ZEBRA_PTM_NOT_READY,
			  "%s: interface %s hasn't passed ptm check",
			  __func__, ifp->name);
		return;
	}
	zebra_interface_up_update(ifp);

	if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(ifp);

	rtadv_if_up(zif);

	/* Install connected routes to the kernel. */
	if (install_connected)
		if_install_connected(ifp);

	/* Handle interface up for specific types for EVPN. Non-VxLAN interfaces
	 * are checked to see if (remote) neighbor entries need to be installed
	 * on them for ARP suppression.
	 */
	if (IS_ZEBRA_IF_VXLAN(ifp))
		zebra_vxlan_if_up(ifp);
	else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
		link_if = ifp;
		zebra_vxlan_svi_up(ifp, link_if);
	} else if (IS_ZEBRA_IF_VLAN(ifp)) {
		link_if = zif->link;
		if (link_if)
			zebra_vxlan_svi_up(ifp, link_if);
	} else if (IS_ZEBRA_IF_MACVLAN(ifp)) {
		zebra_vxlan_macvlan_up(ifp);
	}

	if (zif->es_info.es)
		zebra_evpn_es_if_oper_state_change(zif, true /*up*/);

	if (zif->flags & ZIF_FLAG_EVPN_MH_UPLINK)
		zebra_evpn_mh_uplink_oper_update(zif);

	event_add_timer(zrouter.master, if_zebra_speed_update, ifp, 0,
			&zif->speed_update);
	event_ignore_late_timer(zif->speed_update);
}

/* Interface goes down.  We have to manage different behavior of based
   OS. */
void if_down(struct interface *ifp)
{
	struct zebra_if *zif;
	struct interface *link_if;

	zif = ifp->info;
	zif->down_count++;
	frr_timestamp(2, zif->down_last, sizeof(zif->down_last));

	if_down_nhg_dependents(ifp);

	/* Handle interface down for specific types for EVPN. Non-VxLAN
	 * interfaces
	 * are checked to see if (remote) neighbor entries need to be purged
	 * for ARP suppression.
	 */
	if (IS_ZEBRA_IF_VXLAN(ifp))
		zebra_vxlan_if_down(ifp);
	else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
		link_if = ifp;
		zebra_vxlan_svi_down(ifp, link_if);
	} else if (IS_ZEBRA_IF_VLAN(ifp)) {
		link_if = zif->link;
		if (link_if)
			zebra_vxlan_svi_down(ifp, link_if);
	} else if (IS_ZEBRA_IF_MACVLAN(ifp)) {
		zebra_vxlan_macvlan_down(ifp);
	}

	if (zif->es_info.es)
		zebra_evpn_es_if_oper_state_change(zif, false /*up*/);

	if (zif->flags & ZIF_FLAG_EVPN_MH_UPLINK)
		zebra_evpn_mh_uplink_oper_update(zif);

	/* Notify to the protocol daemons. */
	zebra_interface_down_update(ifp);

	/* Uninstall connected routes from the kernel. */
	if_uninstall_connected(ifp);

	if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);

	/* Delete all neighbor addresses learnt through IPv6 RA */
	if_down_del_nbr_connected(ifp);
}

void if_refresh(struct interface *ifp)
{
#ifndef GNU_LINUX
	if_get_flags(ifp);
#endif
}

void zebra_if_update_link(struct interface *ifp, ifindex_t link_ifindex,
			  ns_id_t ns_id)
{
	struct zebra_if *zif;

	zif = (struct zebra_if *)ifp->info;
	zif->link_nsid = ns_id;
	zif->link_ifindex = link_ifindex;
	zif->link = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
					      link_ifindex);
}

/*
 * during initial link dump kernel does not order lower devices before
 * upper devices so we need to fixup link dependencies at the end of dump
 */
void zebra_if_update_all_links(struct zebra_ns *zns)
{
	struct route_node *rn;
	struct interface *ifp;
	struct zebra_if *zif;

	if (IS_ZEBRA_DEBUG_KERNEL)
		zlog_info("fixup link dependencies");

	for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
		ifp = (struct interface *)rn->info;
		if (!ifp)
			continue;
		zif = ifp->info;
		/* update bond-member to bond linkages */
		if ((IS_ZEBRA_IF_BOND_SLAVE(ifp))
		    && (zif->bondslave_info.bond_ifindex != IFINDEX_INTERNAL)
		    && !zif->bondslave_info.bond_if) {
			if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug("bond mbr %s map to bond %d",
					   zif->ifp->name,
					   zif->bondslave_info.bond_ifindex);
			zebra_l2_map_slave_to_bond(zif, ifp->vrf->vrf_id);
		}

		/* update SVI linkages */
		if ((zif->link_ifindex != IFINDEX_INTERNAL) && !zif->link) {
			zif->link = if_lookup_by_index_per_nsid(
				zif->link_nsid, zif->link_ifindex);
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug("interface %s/%d's lower fixup to %s/%d",
						ifp->name, ifp->ifindex,
						zif->link?zif->link->name:"unk",
						zif->link_ifindex);
		}

		/* Update VLAN<=>SVI map */
		if (IS_ZEBRA_IF_VLAN(ifp))
			zebra_evpn_acc_bd_svi_set(zif, NULL,
						  !!if_is_operative(ifp));
	}
}

static bool if_ignore_set_protodown(const struct interface *ifp, bool new_down,
				    uint32_t new_protodown_rc)
{
	struct zebra_if *zif;
	bool old_down, old_set_down, old_unset_down;

	zif = ifp->info;

	/*
	 * FRR does not have enough data to make this request
	 */
	if (ifp->ifindex == IFINDEX_INTERNAL)
		return true;

	/* Current state as we know it */
	old_down = !!(ZEBRA_IF_IS_PROTODOWN(zif));
	old_set_down = !!CHECK_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
	old_unset_down = !!CHECK_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);

	if (new_protodown_rc == zif->protodown_rc) {
		/* Early return if already down & reason bitfield matches */
		if (new_down == old_down) {
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"Ignoring request to set protodown %s for interface %s (%u): protodown %s is already set (reason bitfield: old 0x%x new 0x%x)",
					new_down ? "on" : "off", ifp->name,
					ifp->ifindex, new_down ? "on" : "off",
					zif->protodown_rc, new_protodown_rc);

			return true;
		}

		/* Early return if already set queued & reason bitfield matches
		 */
		if (new_down && old_set_down) {
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"Ignoring request to set protodown %s for interface %s (%u): protodown %s is already queued to dplane (reason bitfield: old 0x%x new 0x%x)",
					new_down ? "on" : "off", ifp->name,
					ifp->ifindex, new_down ? "on" : "off",
					zif->protodown_rc, new_protodown_rc);

			return true;
		}

		/* Early return if already unset queued & reason bitfield
		 * matches */
		if (!new_down && old_unset_down) {
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"Ignoring request to set protodown %s for interface %s (%u): protodown %s is already queued to dplane (reason bitfield: old 0x%x new 0x%x)",
					new_down ? "on" : "off", ifp->name,
					ifp->ifindex, new_down ? "on" : "off",
					zif->protodown_rc, new_protodown_rc);

			return true;
		}
	}

	return false;
}

int zebra_if_update_protodown_rc(struct interface *ifp, bool new_down,
				 uint32_t new_protodown_rc)
{
	struct zebra_if *zif;

	zif = ifp->info;

	/* Check if we already have this state or it's queued */
	if (if_ignore_set_protodown(ifp, new_down, new_protodown_rc))
		return 1;

	zlog_info(
		"Setting protodown %s - interface %s (%u): reason bitfield change from 0x%x --> 0x%x",
		new_down ? "on" : "off", ifp->name, ifp->ifindex,
		zif->protodown_rc, new_protodown_rc);

	zif->protodown_rc = new_protodown_rc;

	if (new_down)
		SET_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
	else
		SET_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);

#ifdef HAVE_NETLINK
	dplane_intf_update(ifp);
#else
	zlog_warn("Protodown is not supported on this platform");
#endif
	return 0;
}

int zebra_if_set_protodown(struct interface *ifp, bool new_down,
			   enum protodown_reasons new_reason)
{
	struct zebra_if *zif;
	uint32_t new_protodown_rc;

	zif = ifp->info;

	if (new_down)
		new_protodown_rc = zif->protodown_rc | new_reason;
	else
		new_protodown_rc = zif->protodown_rc & ~new_reason;

	return zebra_if_update_protodown_rc(ifp, new_down, new_protodown_rc);
}

/*
 * Handle an interface events based on info in a dplane context object.
 * This runs in the main pthread, using the info in the context object to
 * modify an interface.
 */
static void zebra_if_addr_update_ctx(struct zebra_dplane_ctx *ctx,
				     struct interface *ifp)
{
	uint8_t flags = 0;
	const char *label = NULL;
	uint32_t metric = METRIC_MAX;
	const struct prefix *addr, *dest = NULL;
	enum dplane_op_e op;

	if (!ifp)
		return;

	op = dplane_ctx_get_op(ctx);
	addr = dplane_ctx_get_intf_addr(ctx);

	if (IS_ZEBRA_DEBUG_KERNEL)
		zlog_debug("%s: %s: ifindex %s(%u), addr %pFX", __func__,
			   dplane_op2str(dplane_ctx_get_op(ctx)), ifp->name,
			   ifp->ifindex, addr);

	/* Is there a peer or broadcast address? */
	dest = dplane_ctx_get_intf_dest(ctx);
	if (dest->prefixlen == 0)
		dest = NULL;

	if (dplane_ctx_intf_is_connected(ctx))
		SET_FLAG(flags, ZEBRA_IFA_PEER);

	/* Flags. */
	if (dplane_ctx_intf_is_secondary(ctx))
		SET_FLAG(flags, ZEBRA_IFA_SECONDARY);

	/* Label? */
	if (dplane_ctx_intf_has_label(ctx))
		label = dplane_ctx_get_intf_label(ctx);

	if (label && strcmp(ifp->name, label) == 0)
		label = NULL;

	metric = dplane_ctx_get_intf_metric(ctx);

	/* Register interface address to the interface. */
	if (addr->family == AF_INET) {
		if (op == DPLANE_OP_INTF_ADDR_ADD)
			connected_add_ipv4(
				ifp, flags, &addr->u.prefix4, addr->prefixlen,
				dest ? &dest->u.prefix4 : NULL, label, metric);
		else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
			/* Delete with a peer address */
			connected_delete_ipv4(ifp, flags, &addr->u.prefix4,
					      addr->prefixlen,
					      &dest->u.prefix4);
		} else
			connected_delete_ipv4(ifp, flags, &addr->u.prefix4,
					      addr->prefixlen, NULL);
	}

	if (addr->family == AF_INET6) {
		if (op == DPLANE_OP_INTF_ADDR_ADD) {
			connected_add_ipv6(ifp, flags, &addr->u.prefix6,
					   dest ? &dest->u.prefix6 : NULL,
					   addr->prefixlen, label, metric);
		} else
			connected_delete_ipv6(ifp, &addr->u.prefix6, NULL,
					      addr->prefixlen);
	}

	/*
	 * Linux kernel does not send route delete on interface down/addr del
	 * so we have to re-process routes it owns (i.e. kernel routes)
	 */
	if (op != DPLANE_OP_INTF_ADDR_ADD)
		rib_update(RIB_UPDATE_KERNEL);
}

static void zebra_if_update_ctx(struct zebra_dplane_ctx *ctx,
				struct interface *ifp)
{
	enum zebra_dplane_result dp_res;
	struct zebra_if *zif;
	bool pd_reason_val;
	bool down;

	if (!ifp) {
		if (IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug("%s: Can't find ifp", __func__);

		return;
	}

	dp_res = dplane_ctx_get_status(ctx);
	pd_reason_val = dplane_ctx_get_intf_pd_reason_val(ctx);
	down = dplane_ctx_intf_is_protodown(ctx);

	if (IS_ZEBRA_DEBUG_KERNEL)
		zlog_debug("%s: %s: if %s(%u) ctx-protodown %s ctx-reason %d",
			   __func__, dplane_op2str(dplane_ctx_get_op(ctx)),
			   ifp->name, ifp->ifindex, down ? "on" : "off",
			   pd_reason_val);

	zif = ifp->info;
	if (!zif) {
		if (IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug("%s: if %s(%u) zebra info pointer is NULL",
				   __func__, ifp->name, ifp->ifindex);
		return;
	}

	if (dp_res != ZEBRA_DPLANE_REQUEST_SUCCESS) {
		if (IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug("%s: if %s(%u) dplane update failed",
				   __func__, ifp->name, ifp->ifindex);
		goto done;
	}

	/* Update our info */
	COND_FLAG(zif->flags, ZIF_FLAG_PROTODOWN, down);

done:
	/* Clear our dplane flags */
	UNSET_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
	UNSET_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);
}

/*
 * Handle netconf change from a dplane context object; runs in the main
 * pthread so it can update zebra data structs.
 */
static void zebra_if_netconf_update_ctx(struct zebra_dplane_ctx *ctx,
					struct interface *ifp,
					ifindex_t ifindex)
{
	struct zebra_if *zif = NULL;
	afi_t afi;
	enum dplane_netconf_status_e mpls, mcast_on, linkdown;
	bool *mcast_set, *linkdown_set;

	if (!ifp && ifindex != -1 && ifindex != -2) {
		if (IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug("%s: Can't find ifp(%u)", __func__, ifindex);

		return;
	}

	afi = dplane_ctx_get_afi(ctx);
	mpls = dplane_ctx_get_netconf_mpls(ctx);
	linkdown = dplane_ctx_get_netconf_linkdown(ctx);
	mcast_on = dplane_ctx_get_netconf_mcast(ctx);

	if (ifindex == DPLANE_NETCONF_IFINDEX_ALL) {
		if (afi == AFI_IP) {
			mcast_set = &zrouter.all_mc_forwardingv4;
			linkdown_set = &zrouter.all_linkdownv4;
		} else {
			mcast_set = &zrouter.all_mc_forwardingv6;
			linkdown_set = &zrouter.all_linkdownv6;
		}
	} else if (ifindex == DPLANE_NETCONF_IFINDEX_DEFAULT) {
		if (afi == AFI_IP) {
			mcast_set = &zrouter.default_mc_forwardingv4;
			linkdown_set = &zrouter.default_linkdownv4;
		} else {
			mcast_set = &zrouter.default_mc_forwardingv6;
			linkdown_set = &zrouter.default_linkdownv6;
		}
	} else {
		zif = ifp->info;
		if (!zif) {
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"%s: if %s(%u) zebra info pointer is NULL",
					__func__, ifp ? ifp->name : "(null)",
					ifp ? ifp->ifindex : ifindex);
			return;
		}
		if (afi == AFI_IP) {
			mcast_set = &zif->v4mcast_on;
			linkdown_set = &zif->linkdown;
		} else {
			mcast_set = &zif->v6mcast_on;
			linkdown_set = &zif->linkdownv6;
		}

		/*
		 * mpls netconf data is neither v4 or v6 it's AF_MPLS!
		 */
		if (mpls == DPLANE_NETCONF_STATUS_ENABLED) {
			zif->mpls = true;
			zebra_mpls_turned_on();
		} else if (mpls == DPLANE_NETCONF_STATUS_DISABLED)
			zif->mpls = false;
	}

	if (linkdown == DPLANE_NETCONF_STATUS_ENABLED)
		*linkdown_set = true;
	else if (linkdown == DPLANE_NETCONF_STATUS_DISABLED)
		*linkdown_set = false;

	if (mcast_on == DPLANE_NETCONF_STATUS_ENABLED)
		*mcast_set = true;
	else if (mcast_on == DPLANE_NETCONF_STATUS_DISABLED)
		*mcast_set = false;

	if (IS_ZEBRA_DEBUG_KERNEL)
		zlog_debug(
			"%s: afi: %d if %s, ifindex %d, mpls %s mc_forwarding: %s linkdown %s",
			__func__, afi, ifp ? ifp->name : "Global",
			ifp ? ifp->ifindex : ifindex,
			(zif ? (zif->mpls ? "ON" : "OFF") : "OFF"),
			(*mcast_set ? "ON" : "OFF"),
			(*linkdown_set ? "ON" : "OFF"));
}

static void interface_vrf_change(enum dplane_op_e op, ifindex_t ifindex,
				 const char *name, uint32_t tableid,
				 ns_id_t ns_id)
{
	struct vrf *vrf;
	struct zebra_vrf *zvrf = NULL;

	if (op == DPLANE_OP_INTF_DELETE) {
		if (IS_ZEBRA_DEBUG_DPLANE)
			zlog_debug("DPLANE_OP_INTF_DELETE for VRF %s(%u)", name,
				   ifindex);

		vrf = vrf_lookup_by_id((vrf_id_t)ifindex);
		if (!vrf) {
			flog_warn(EC_ZEBRA_VRF_NOT_FOUND,
				  "%s(%u): vrf not found", name, ifindex);
			return;
		}

		vrf_delete(vrf);
	} else {
		if (IS_ZEBRA_DEBUG_DPLANE)
			zlog_debug(
				"DPLANE_OP_INTF_UPDATE for VRF %s(%u) table %u",
				name, ifindex, tableid);

		if (!vrf_lookup_by_id((vrf_id_t)ifindex)) {
			vrf_id_t exist_id;

			exist_id = zebra_vrf_lookup_by_table(tableid, ns_id);
			if (exist_id != VRF_DEFAULT) {
				vrf = vrf_lookup_by_id(exist_id);

				if (vrf)
					flog_err(EC_ZEBRA_VRF_MISCONFIGURED,
						 "VRF %s id %u table id overlaps existing vrf %s(%d), misconfiguration exiting",
						 name, ifindex, vrf->name,
						 vrf->vrf_id);
				else
					flog_err(EC_ZEBRA_VRF_NOT_FOUND,
						 "VRF %s id %u does not exist",
						 name, ifindex);

				exit(-1);
			}
		}

		vrf = vrf_update((vrf_id_t)ifindex, name);
		if (!vrf) {
			flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
				 name, ifindex);
			return;
		}

		/*
		 * This is the only place that we get the actual kernel table_id
		 * being used.  We need it to set the table_id of the routes
		 * we are passing to the kernel.... And to throw some totally
		 * awesome parties. that too.
		 *
		 * At this point we *must* have a zvrf because the vrf_create
		 * callback creates one.  We *must* set the table id
		 * before the vrf_enable because of( at the very least )
		 * static routes being delayed for installation until
		 * during the vrf_enable callbacks.
		 */
		zvrf = (struct zebra_vrf *)vrf->info;
		zvrf->table_id = tableid;

		/* Enable the created VRF. */
		if (!vrf_enable(vrf)) {
			flog_err(EC_LIB_INTERFACE,
				 "Failed to enable VRF %s id %u", name,
				 ifindex);
			return;
		}
	}
}

/*
 *  Note: on netlink systems, there should be a 1-to-1 mapping
 * between interface names and ifindex values.
 */
static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
			struct zebra_ns *zns)
{
	struct interface *oifp;

	oifp = if_lookup_by_index_per_ns(zns, ifi_index);
	if ((oifp != NULL) && (oifp != ifp)) {
		if (ifi_index == IFINDEX_INTERNAL)
			flog_err(
				EC_LIB_INTERFACE,
				"Netlink is setting interface %s ifindex to reserved internal value %u",
				ifp->name, ifi_index);
		else {
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"interface index %d was renamed from %s to %s",
					ifi_index, oifp->name, ifp->name);
			if (if_is_up(oifp))
				flog_err(
					EC_LIB_INTERFACE,
					"interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
					ifi_index, oifp->name, ifp->name);
			if_delete_update(&oifp);
		}
	}
	if_set_index(ifp, ifi_index);
}

static inline void zebra_if_set_ziftype(struct interface *ifp,
					enum zebra_iftype zif_type,
					enum zebra_slave_iftype zif_slave_type)
{
	struct zebra_if *zif;

	zif = (struct zebra_if *)ifp->info;
	zif->zif_slave_type = zif_slave_type;

	if (zif->zif_type != zif_type) {
		zif->zif_type = zif_type;
		/* If the if_type has been set to bond initialize ES info
		 * against it. XXX - note that we don't handle the case where
		 * a zif changes from bond to non-bond; it is really
		 * an unexpected/error condition.
		 */
		zebra_evpn_if_init(zif);
	}
}

static void interface_update_hw_addr(struct zebra_dplane_ctx *ctx,
				     struct interface *ifp)
{
	int i;

	ifp->hw_addr_len = dplane_ctx_get_ifp_hw_addr_len(ctx);
	memcpy(ifp->hw_addr, dplane_ctx_get_ifp_hw_addr(ctx), ifp->hw_addr_len);

	for (i = 0; i < ifp->hw_addr_len; i++)
		if (ifp->hw_addr[i] != 0)
			break;

	if (i == ifp->hw_addr_len)
		ifp->hw_addr_len = 0;
}

static void interface_update_l2info(struct zebra_dplane_ctx *ctx,
				    struct interface *ifp,
				    enum zebra_iftype zif_type, int add,
				    ns_id_t link_nsid)
{
	const struct zebra_l2info_vxlan *vxlan_info;
	const struct zebra_l2info_gre *gre_info;

	switch (zif_type) {
	case ZEBRA_IF_BRIDGE:
		zebra_l2_bridge_add_update(ifp,
					   dplane_ctx_get_ifp_bridge_info(ctx));
		break;
	case ZEBRA_IF_VLAN:
		zebra_l2_vlanif_update(ifp, dplane_ctx_get_ifp_vlan_info(ctx));
		zebra_evpn_acc_bd_svi_set(ifp->info, NULL,
					  !!if_is_operative(ifp));
		break;
	case ZEBRA_IF_VXLAN:
		vxlan_info = dplane_ctx_get_ifp_vxlan_info(ctx);
		zebra_l2_vxlanif_add_update(ifp, vxlan_info, add);
		if (link_nsid != NS_UNKNOWN && vxlan_info->ifindex_link)
			zebra_if_update_link(ifp, vxlan_info->ifindex_link,
					     link_nsid);
		break;
	case ZEBRA_IF_GRE:
		gre_info = dplane_ctx_get_ifp_gre_info(ctx);
		zebra_l2_greif_add_update(ifp, gre_info, add);
		if (link_nsid != NS_UNKNOWN && gre_info->ifindex_link)
			zebra_if_update_link(ifp, gre_info->ifindex_link,
					     link_nsid);
		break;
	case ZEBRA_IF_OTHER:
	case ZEBRA_IF_VRF:
	case ZEBRA_IF_MACVLAN:
	case ZEBRA_IF_VETH:
	case ZEBRA_IF_BOND:
		break;
	}
}

static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield)
{
	uint8_t frr_protodown_r_bit = if_netlink_get_frr_protodown_r_bit();

	return (rc_bitfield == (((uint32_t)1) << frr_protodown_r_bit));
}

static void interface_if_protodown(struct interface *ifp, bool protodown,
				   uint32_t rc_bitfield)
{
	struct zebra_if *zif = ifp->info;
	bool old_protodown;

	/*
	 * Set our reason code to note it wasn't us.
	 * If the reason we got from the kernel is ONLY frr though, don't
	 * set it.
	 */
	COND_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_EXTERNAL,
		  protodown && rc_bitfield &&
			  !is_if_protodown_reason_only_frr(rc_bitfield));


	old_protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
	if (protodown == old_protodown)
		return;

	if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_DPLANE)
		zlog_debug("interface %s dplane change, protodown %s",
			   ifp->name, protodown ? "on" : "off");

	/* Set protodown, respectively */
	COND_FLAG(zif->flags, ZIF_FLAG_PROTODOWN, protodown);

	if (zebra_evpn_is_es_bond_member(ifp)) {
		/* Check it's not already being sent to the dplane first */
		if (protodown &&
		    CHECK_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN)) {
			if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
					ifp->name);
			return;
		}

		if (!protodown &&
		    CHECK_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN)) {
			if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
					ifp->name);
			return;
		}

		if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug(
				"bond mbr %s reinstate protodown %s in the dplane",
				ifp->name, old_protodown ? "on" : "off");

		if (old_protodown)
			SET_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
		else
			SET_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);

		dplane_intf_update(zif->ifp);
	}
}

static void if_sweep_protodown(struct zebra_if *zif)
{
	bool protodown;

	protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);

	if (!protodown)
		return;

	if (IS_ZEBRA_DEBUG_KERNEL)
		zlog_debug("interface %s sweeping protodown %s reason 0x%x",
			   zif->ifp->name, protodown ? "on" : "off",
			   zif->protodown_rc);

	/* Only clear our reason codes, leave external if it was set */
	UNSET_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_ALL);
	dplane_intf_update(zif->ifp);
}

static void
interface_bridge_vxlan_vlan_vni_map_update(struct zebra_dplane_ctx *ctx,
					   struct interface *ifp)
{
	const struct zebra_vxlan_vni_array *vniarray =
		dplane_ctx_get_ifp_vxlan_vni_array(ctx);
	struct zebra_vxlan_vni vni_start, vni_end;
	struct hash *vni_table = NULL;
	struct zebra_vxlan_vni vni, *vnip;
	vni_t vni_id;
	vlanid_t vid;
	int i;

	memset(&vni_start, 0, sizeof(vni_start));
	memset(&vni_end, 0, sizeof(vni_end));

	for (i = 0; i < vniarray->count; i++) {
		uint16_t flags = vniarray->vnis[i].flags;

		if (flags & DPLANE_BRIDGE_VLAN_INFO_RANGE_BEGIN) {
			vni_start = vniarray->vnis[i];
			continue;
		}

		if (flags & DPLANE_BRIDGE_VLAN_INFO_RANGE_END)
			vni_end = vniarray->vnis[i];

		if (!(flags & DPLANE_BRIDGE_VLAN_INFO_RANGE_END)) {
			vni_start = vniarray->vnis[i];
			vni_end = vniarray->vnis[i];
		}

		if (IS_ZEBRA_DEBUG_DPLANE)
			zlog_debug(
				"Vlan-Vni(%d:%d-%d:%d) update for VxLAN IF %s(%u)",
				vni_start.access_vlan, vni_end.access_vlan,
				vni_start.vni, vni_end.vni, ifp->name,
				ifp->ifindex);

		if (!vni_table) {
			vni_table = zebra_vxlan_vni_table_create();
			if (!vni_table)
				return;
		}

		for (vid = vni_start.access_vlan, vni_id = vni_start.vni;
		     vid <= vni_end.access_vlan; vid++, vni_id++) {

			memset(&vni, 0, sizeof(vni));
			vni.vni = vni_id;
			vni.access_vlan = vid;
			vnip = hash_get(vni_table, &vni, zebra_vxlan_vni_alloc);
			if (!vnip)
				return;
		}

		memset(&vni_start, 0, sizeof(vni_start));
		memset(&vni_end, 0, sizeof(vni_end));
	}

	if (vni_table)
		zebra_vxlan_if_vni_table_add_update(ifp, vni_table);
}

static void interface_bridge_vxlan_update(struct zebra_dplane_ctx *ctx,
					  struct interface *ifp)
{
	struct zebra_if *zif = ifp->info;
	const struct zebra_dplane_bridge_vlan_info *bvinfo;

	if (dplane_ctx_get_ifp_no_afspec(ctx))
		return;

	if (IS_ZEBRA_VXLAN_IF_SVD(zif))
		interface_bridge_vxlan_vlan_vni_map_update(ctx, ifp);

	if (dplane_ctx_get_ifp_no_bridge_vlan_info(ctx))
		return;

	bvinfo = dplane_ctx_get_ifp_bridge_vlan_info(ctx);

	if (!(bvinfo->flags & DPLANE_BRIDGE_VLAN_INFO_PVID))
		return;

	if (IS_ZEBRA_DEBUG_DPLANE)
		zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", bvinfo->vid,
			   ifp->name, ifp->ifindex);

	zebra_l2_vxlanif_update_access_vlan(ifp, bvinfo->vid);
}

static void interface_bridge_vlan_update(struct zebra_dplane_ctx *ctx,
					 struct interface *ifp)
{
	struct zebra_if *zif = ifp->info;
	const struct zebra_dplane_bridge_vlan_info_array *bvarray;
	struct zebra_dplane_bridge_vlan_info bvinfo;
	bitfield_t old_vlan_bitmap;
	uint16_t vid_range_start = 0;
	int32_t i;

	/* cache the old bitmap addrs */
	old_vlan_bitmap = zif->vlan_bitmap;
	/* create a new bitmap space for re-eval */
	bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX);

	/* Could we have multiple bridge vlan infos? */
	bvarray = dplane_ctx_get_ifp_bridge_vlan_info_array(ctx);
	if (!bvarray)
		return;

	for (i = 0; i < bvarray->count; i++) {
		bvinfo = bvarray->array[i];

		if (bvinfo.flags & DPLANE_BRIDGE_VLAN_INFO_RANGE_BEGIN) {
			vid_range_start = bvinfo.vid;
			continue;
		}

		if (!(bvinfo.flags & DPLANE_BRIDGE_VLAN_INFO_RANGE_END))
			vid_range_start = bvinfo.vid;

		zebra_vlan_bitmap_compute(ifp, vid_range_start, bvinfo.vid);
	}

	zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap);
	bf_free(old_vlan_bitmap);
}

static void interface_bridge_handling(struct zebra_dplane_ctx *ctx,
				      struct interface *ifp,
				      enum zebra_iftype zif_type)
{
	struct zebra_if *zif;

	if (!ifp) {
		zlog_warn("Cannot find bridge if %s(%u)",
			  dplane_ctx_get_ifname(ctx),
			  dplane_ctx_get_ifindex(ctx));
		return;
	}

	if (IS_ZEBRA_IF_VXLAN(ifp))
		return interface_bridge_vxlan_update(ctx, ifp);

	/*
	 * build vlan bitmap associated with this interface if that
	 * device type is interested in the vlans
	 */
	zif = ifp->info;
	if (bf_is_inited(zif->vlan_bitmap))
		interface_bridge_vlan_update(ctx, ifp);
}

static void zebra_if_dplane_ifp_handling(struct zebra_dplane_ctx *ctx)
{
	enum dplane_op_e op = dplane_ctx_get_op(ctx);
	const char *name = dplane_ctx_get_ifname(ctx);
	ns_id_t ns_id = dplane_ctx_get_ns_id(ctx);
	ifindex_t ifindex = dplane_ctx_get_ifindex(ctx);
	ifindex_t bond_ifindex = dplane_ctx_get_ifp_bond_ifindex(ctx);
	uint32_t tableid = dplane_ctx_get_ifp_table_id(ctx);
	enum zebra_iftype zif_type = dplane_ctx_get_ifp_zif_type(ctx);
	struct interface *ifp;
	struct zebra_ns *zns;

	zns = zebra_ns_lookup(ns_id);
	if (!zns) {
		zlog_err("Where is our namespace?");
		return;
	}

	if (IS_ZEBRA_DEBUG_DPLANE)
		zlog_debug("%s for %s(%u)", dplane_op2str(op), name, ifindex);

	ifp = if_lookup_by_name_per_ns(zns, name);
	if (op == DPLANE_OP_INTF_DELETE) {
		/* Delete interface notification from kernel */
		if (ifp == NULL) {
			if (IS_ZEBRA_DEBUG_EVENT)
				zlog_debug(
					"Delete LINK received for unknown interface %s(%u)",
					name, ifindex);
			return;
		}

		if (IS_ZEBRA_IF_BOND(ifp))
			zebra_l2if_update_bond(ifp, false);
		if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
			zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
		/* Special handling for bridge or VxLAN interfaces. */
		if (IS_ZEBRA_IF_BRIDGE(ifp))
			zebra_l2_bridge_del(ifp);
		else if (IS_ZEBRA_IF_VXLAN(ifp))
			zebra_l2_vxlanif_del(ifp);

		if_delete_update(&ifp);

		if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
			interface_vrf_change(op, ifindex, name, tableid, ns_id);
	} else {
		ifindex_t master_ifindex, bridge_ifindex, bond_ifindex,
			link_ifindex;
		enum zebra_slave_iftype zif_slave_type;
		uint8_t bypass;
		uint64_t flags;
		vrf_id_t vrf_id;
		uint32_t mtu;
		ns_id_t link_nsid;
		struct zebra_if *zif;
		bool protodown, protodown_set, startup;
		uint32_t rc_bitfield;
		uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
		char *desc;
		uint8_t family;

		/* If VRF, create or update the VRF structure itself. */
		if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
			interface_vrf_change(op, ifindex, name, tableid, ns_id);

		master_ifindex = dplane_ctx_get_ifp_master_ifindex(ctx);
		zif_slave_type = dplane_ctx_get_ifp_zif_slave_type(ctx);
		bridge_ifindex = dplane_ctx_get_ifp_bridge_ifindex(ctx);
		bond_ifindex = dplane_ctx_get_ifp_bond_ifindex(ctx);
		bypass = dplane_ctx_get_ifp_bypass(ctx);
		flags = dplane_ctx_get_ifp_flags(ctx);
		vrf_id = dplane_ctx_get_ifp_vrf_id(ctx);
		mtu = dplane_ctx_get_ifp_mtu(ctx);
		link_ifindex = dplane_ctx_get_ifp_link_ifindex(ctx);
		link_nsid = dplane_ctx_get_ifp_link_nsid(ctx);
		protodown_set = dplane_ctx_get_ifp_protodown_set(ctx);
		protodown = dplane_ctx_get_ifp_protodown(ctx);
		rc_bitfield = dplane_ctx_get_ifp_rc_bitfield(ctx);
		startup = dplane_ctx_get_ifp_startup(ctx);
		desc = dplane_ctx_get_ifp_desc(ctx);
		family = dplane_ctx_get_ifp_family(ctx);

#ifndef AF_BRIDGE
		/*
		 * Work around to make free bsd happy at the moment
		 */
#define AF_BRIDGE 7
#endif
		if (family == AF_BRIDGE)
			return interface_bridge_handling(ctx, ifp, zif_type);

		if (ifp == NULL ||
		    !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
			/* Add interface notification from kernel */
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u",
					name, ifindex, vrf_id, zif_type,
					zif_slave_type, master_ifindex);

			if (ifp == NULL) {
				/* unknown interface */
				ifp = if_get_by_name(name, vrf_id, NULL);
			} else {
				/* pre-configured interface, learnt now */
				if (ifp->vrf->vrf_id != vrf_id)
					if_update_to_new_vrf(ifp, vrf_id);
			}

			zif = ifp->info;

			/* Update interface information. */
			set_ifindex(ifp, ifindex, zns);
			ifp->flags = flags;
			ifp->mtu6 = ifp->mtu = mtu;
			ifp->metric = 0;
			ifp->speed = kernel_get_speed(ifp, NULL);
			ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
			ifp->txqlen = dplane_ctx_get_intf_txqlen(ctx);

			/* Set interface type */
			zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
			if (IS_ZEBRA_IF_VRF(ifp))
				SET_FLAG(ifp->status,
					 ZEBRA_INTERFACE_VRF_LOOPBACK);

			/* Update link. */
			zebra_if_update_link(ifp, link_ifindex, link_nsid);

			ifp->ll_type = dplane_ctx_get_ifp_zltype(ctx);
			interface_update_hw_addr(ctx, ifp);

			/* Inform clients, install any configured addresses. */
			if_add_update(ifp);

			/*
			 * Extract and save L2 interface information, take
			 * additional actions.
			 */
			interface_update_l2info(ctx, ifp, zif_type, 1,
						link_nsid);
			if (IS_ZEBRA_IF_BOND(ifp))
				zebra_l2if_update_bond(ifp, true);
			if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
				zebra_l2if_update_bridge_slave(
					ifp, bridge_ifindex, ns_id,
					ZEBRA_BRIDGE_NO_ACTION);
			else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
				zebra_l2if_update_bond_slave(ifp, bond_ifindex,
							     !!bypass);

			if (protodown_set) {
				interface_if_protodown(ifp, protodown,
						       rc_bitfield);
				if (startup)
					if_sweep_protodown(zif);
			}

			if (IS_ZEBRA_IF_BRIDGE(ifp)) {
				if (IS_ZEBRA_DEBUG_KERNEL)
					zlog_debug(
						"RTM_NEWLINK ADD for %s(%u), vlan-aware %d",
						name, ifp->ifindex,
						IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(
							zif));
			}
		} else if (ifp->vrf->vrf_id != vrf_id) {
			/* VRF change for an interface. */
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u",
					name, ifp->ifindex, ifp->vrf->vrf_id,
					vrf_id);

			if_handle_vrf_change(ifp, vrf_id);
		} else {
			bool was_bridge_slave, was_bond_slave;
			uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;

			zif = ifp->info;

			/* Interface update. */
			if (IS_ZEBRA_DEBUG_KERNEL)
				zlog_debug(
					"RTM_NEWLINK update for %s(%u) sl_type %d master %u",
					name, ifp->ifindex, zif_slave_type,
					master_ifindex);

			set_ifindex(ifp, ifindex, zns);
			ifp->mtu6 = ifp->mtu = mtu;
			ifp->metric = 0;
			ifp->txqlen = dplane_ctx_get_intf_txqlen(ctx);

			/*
			 * Update interface type - NOTE: Only slave_type can
			 * change.
			 */
			was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
			was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
			zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);

			memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);

			/* Update link. */
			zebra_if_update_link(ifp, link_ifindex, link_nsid);

			ifp->ll_type = dplane_ctx_get_ifp_zltype(ctx);
			interface_update_hw_addr(ctx, ifp);

			if (protodown_set)
				interface_if_protodown(ifp, protodown,
						       rc_bitfield);

			if (if_is_no_ptm_operative(ifp)) {
				bool is_up = if_is_operative(ifp);

				ifp->flags = flags;
				if (!if_is_no_ptm_operative(ifp) ||
				    CHECK_FLAG(zif->flags,
					       ZIF_FLAG_PROTODOWN)) {
					if (IS_ZEBRA_DEBUG_KERNEL)
						zlog_debug(
							"Intf %s(%u) has gone DOWN",
							name, ifp->ifindex);
					if_down(ifp);
					rib_update(RIB_UPDATE_KERNEL);
				} else if (if_is_operative(ifp)) {
					bool mac_updated = false;

					/*
					 * Must notify client daemons of new
					 * interface status.
					 */
					if (IS_ZEBRA_DEBUG_KERNEL)
						zlog_debug(
							"Intf %s(%u) PTM up, notifying clients",
							name, ifp->ifindex);
					if_up(ifp, !is_up);

					/*
					 * Update EVPN VNI when SVI MAC change
					 */
					if (memcmp(old_hw_addr, ifp->hw_addr,
						   INTERFACE_HWADDR_MAX))
						mac_updated = true;
					if (IS_ZEBRA_IF_VLAN(ifp) &&
					    mac_updated) {
						struct interface *link_if;

						link_if = if_lookup_by_index_per_ns(
							zebra_ns_lookup(
								NS_DEFAULT),
							link_ifindex);
						if (link_if)
							zebra_vxlan_svi_up(
								ifp, link_if);
					} else if (mac_updated &&
						   IS_ZEBRA_IF_BRIDGE(ifp)) {
						zlog_debug(
							"Intf %s(%u) bridge changed MAC address",
							name, ifp->ifindex);
						chgflags =
							ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
					}
				}
			} else {
				ifp->flags = flags;
				if (if_is_operative(ifp) &&
				    !CHECK_FLAG(zif->flags,
						ZIF_FLAG_PROTODOWN)) {
					if (IS_ZEBRA_DEBUG_KERNEL)
						zlog_debug(
							"Intf %s(%u) has come UP",
							name, ifp->ifindex);
					if_up(ifp, true);
					if (IS_ZEBRA_IF_BRIDGE(ifp))
						chgflags =
							ZEBRA_BRIDGE_MASTER_UP;
				} else {
					if (IS_ZEBRA_DEBUG_KERNEL)
						zlog_debug(
							"Intf %s(%u) has gone DOWN",
							name, ifp->ifindex);
					if_down(ifp);
					rib_update(RIB_UPDATE_KERNEL);
				}
			}

			/*
			 * Extract and save L2 interface information, take
			 * additional actions.
			 */
			interface_update_l2info(ctx, ifp, zif_type, 0,
						link_nsid);
			if (IS_ZEBRA_IF_BRIDGE(ifp))
				zebra_l2if_update_bridge(ifp, chgflags);
			if (IS_ZEBRA_IF_BOND(ifp))
				zebra_l2if_update_bond(ifp, true);
			if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
				zebra_l2if_update_bridge_slave(
					ifp, bridge_ifindex, ns_id, chgflags);
			else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
				zebra_l2if_update_bond_slave(ifp, bond_ifindex,
							     !!bypass);
			if (IS_ZEBRA_IF_BRIDGE(ifp)) {
				if (IS_ZEBRA_DEBUG_KERNEL)
					zlog_debug(
						"RTM_NEWLINK update for %s(%u), vlan-aware %d",
						name, ifp->ifindex,
						IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(
							zif));
			}
		}

		zif = ifp->info;
		if (zif) {
			XFREE(MTYPE_ZIF_DESC, zif->desc);
			if (desc[0])
				zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
		}
	}
}

void zebra_if_dplane_result(struct zebra_dplane_ctx *ctx)
{
	struct zebra_ns *zns;
	struct interface *ifp;
	ns_id_t ns_id;
	enum dplane_op_e op;
	enum zebra_dplane_result dp_res;
	ifindex_t ifindex;

	ns_id = dplane_ctx_get_ns_id(ctx);
	dp_res = dplane_ctx_get_status(ctx);
	op = dplane_ctx_get_op(ctx);
	ifindex = dplane_ctx_get_ifindex(ctx);

	if (IS_ZEBRA_DEBUG_DPLANE_DETAIL || IS_ZEBRA_DEBUG_KERNEL)
		zlog_debug("Intf dplane ctx %p, op %s, ifindex (%u), result %s",
			   ctx, dplane_op2str(op), ifindex,
			   dplane_res2str(dp_res));

	zns = zebra_ns_lookup(ns_id);
	if (zns == NULL) {
		/* No ns - deleted maybe? */
		if (IS_ZEBRA_DEBUG_KERNEL)
			zlog_debug("%s: can't find zns id %u", __func__, ns_id);

		return;
	}

	ifp = if_lookup_by_index_per_ns(zns, ifindex);

	switch (op) {
	case DPLANE_OP_INTF_ADDR_ADD:
	case DPLANE_OP_INTF_ADDR_DEL:
		zebra_if_addr_update_ctx(ctx, ifp);
		break;

	case DPLANE_OP_INTF_INSTALL:
	case DPLANE_OP_INTF_UPDATE:
	case DPLANE_OP_INTF_DELETE:
		/*
		 * Queued from the dplane means it is something
		 * that we need to handle( create/delete the
		 * interface as needed )
		 */
		if (dp_res == ZEBRA_DPLANE_REQUEST_QUEUED)
			zebra_if_dplane_ifp_handling(ctx);
		else
			zebra_if_update_ctx(ctx, ifp);
		break;

	case DPLANE_OP_INTF_NETCONFIG:
		zebra_if_netconf_update_ctx(ctx, ifp, ifindex);
		break;

	case DPLANE_OP_ROUTE_INSTALL:
	case DPLANE_OP_ROUTE_UPDATE:
	case DPLANE_OP_ROUTE_DELETE:
	case DPLANE_OP_NH_DELETE:
	case DPLANE_OP_NH_INSTALL:
	case DPLANE_OP_NH_UPDATE:
	case DPLANE_OP_ROUTE_NOTIFY:
	case DPLANE_OP_LSP_INSTALL:
	case DPLANE_OP_LSP_UPDATE:
	case DPLANE_OP_LSP_DELETE:
	case DPLANE_OP_LSP_NOTIFY:
	case DPLANE_OP_PW_INSTALL:
	case DPLANE_OP_PW_UNINSTALL:
	case DPLANE_OP_SYS_ROUTE_ADD:
	case DPLANE_OP_SYS_ROUTE_DELETE:
	case DPLANE_OP_ADDR_INSTALL:
	case DPLANE_OP_ADDR_UNINSTALL:
	case DPLANE_OP_MAC_INSTALL:
	case DPLANE_OP_MAC_DELETE:
	case DPLANE_OP_NEIGH_INSTALL:
	case DPLANE_OP_NEIGH_UPDATE:
	case DPLANE_OP_NEIGH_DELETE:
	case DPLANE_OP_NEIGH_IP_INSTALL:
	case DPLANE_OP_NEIGH_IP_DELETE:
	case DPLANE_OP_VTEP_ADD:
	case DPLANE_OP_VTEP_DELETE:
	case DPLANE_OP_RULE_ADD:
	case DPLANE_OP_RULE_DELETE:
	case DPLANE_OP_RULE_UPDATE:
	case DPLANE_OP_NEIGH_DISCOVER:
	case DPLANE_OP_BR_PORT_UPDATE:
	case DPLANE_OP_NONE:
	case DPLANE_OP_IPTABLE_ADD:
	case DPLANE_OP_IPTABLE_DELETE:
	case DPLANE_OP_IPSET_ADD:
	case DPLANE_OP_IPSET_DELETE:
	case DPLANE_OP_IPSET_ENTRY_ADD:
	case DPLANE_OP_IPSET_ENTRY_DELETE:
	case DPLANE_OP_NEIGH_TABLE_UPDATE:
	case DPLANE_OP_GRE_SET:
	case DPLANE_OP_TC_QDISC_INSTALL:
	case DPLANE_OP_TC_QDISC_UNINSTALL:
	case DPLANE_OP_TC_CLASS_ADD:
	case DPLANE_OP_TC_CLASS_DELETE:
	case DPLANE_OP_TC_CLASS_UPDATE:
	case DPLANE_OP_TC_FILTER_ADD:
	case DPLANE_OP_TC_FILTER_DELETE:
	case DPLANE_OP_TC_FILTER_UPDATE:
	case DPLANE_OP_STARTUP_STAGE:
		break; /* should never hit here */
	}
}

/* Dump if address information to vty. */
static void connected_dump_vty(struct vty *vty, json_object *json,
			       struct connected *connected)
{
	struct prefix *p;
	json_object *json_addr = NULL;

	/* Print interface address. */
	p = connected->address;

	if (json) {
		json_addr = json_object_new_object();
		json_object_array_add(json, json_addr);
		json_object_string_addf(json_addr, "address", "%pFX", p);
	} else {
		vty_out(vty, "  %s %pFX", prefix_family_str(p), p);
	}

	/* If there is destination address, print it. */
	if (CONNECTED_PEER(connected) && connected->destination) {
		if (json) {
			json_object_string_addf(json_addr, "peer", "%pFX",
						connected->destination);
		} else {
			vty_out(vty, " peer %pFX", connected->destination);
		}
	}

	if (json)
		json_object_boolean_add(
			json_addr, "secondary",
			CHECK_FLAG(connected->flags, ZEBRA_IFA_SECONDARY));
	else if (CHECK_FLAG(connected->flags, ZEBRA_IFA_SECONDARY))
		vty_out(vty, " secondary");

	if (json)
		json_object_boolean_add(
			json_addr, "unnumbered",
			CHECK_FLAG(connected->flags, ZEBRA_IFA_UNNUMBERED));
	else if (CHECK_FLAG(connected->flags, ZEBRA_IFA_UNNUMBERED))
		vty_out(vty, " unnumbered");

	if (connected->label) {
		if (json)
			json_object_string_add(json_addr, "label",
					       connected->label);
		else
			vty_out(vty, " %s", connected->label);
	}

	if (!json)
		vty_out(vty, "\n");
}

/* Dump interface neighbor address information to vty. */
static void nbr_connected_dump_vty(struct vty *vty, json_object *json,
				   struct nbr_connected *connected)
{
	struct prefix *p;
	char buf[PREFIX2STR_BUFFER];

	/* Print interface address. */
	p = connected->address;
	if (json)
		json_array_string_add(json, prefix2str(p, buf, sizeof(buf)));
	else
		vty_out(vty, "  %s %pFX\n", prefix_family_str(p), p);
}

static const char *
zebra_zifslavetype_2str(enum zebra_slave_iftype zif_slave_type)
{
	switch (zif_slave_type) {
	case ZEBRA_IF_SLAVE_BRIDGE:
		return "Bridge";
	case ZEBRA_IF_SLAVE_VRF:
		return "Vrf";
	case ZEBRA_IF_SLAVE_BOND:
		return "Bond";
	case ZEBRA_IF_SLAVE_OTHER:
		return "Other";
	case ZEBRA_IF_SLAVE_NONE:
		return "None";
	}
	return "None";
}

static const char *zebra_ziftype_2str(enum zebra_iftype zif_type)
{
	switch (zif_type) {
	case ZEBRA_IF_OTHER:
		return "Other";

	case ZEBRA_IF_BRIDGE:
		return "Bridge";

	case ZEBRA_IF_VLAN:
		return "Vlan";

	case ZEBRA_IF_VXLAN:
		return "Vxlan";

	case ZEBRA_IF_VRF:
		return "VRF";

	case ZEBRA_IF_VETH:
		return "VETH";

	case ZEBRA_IF_BOND:
		return "bond";

	case ZEBRA_IF_MACVLAN:
		return "macvlan";

	case ZEBRA_IF_GRE:
		return "GRE";

	default:
		return "Unknown";
	}
}

/* Interface's brief information print out to vty interface. */
static void ifs_dump_brief_vty(struct vty *vty, struct vrf *vrf)
{
	struct connected *connected;
	struct listnode *node;
	struct route_node *rn;
	struct zebra_if *zebra_if;
	struct prefix *p;
	struct interface *ifp;
	bool print_header = true;

	FOR_ALL_INTERFACES (vrf, ifp) {
		bool first_pfx_printed = false;

		if (print_header) {
			vty_out(vty, "%-16s%-8s%-16s%s\n", "Interface",
				"Status", "VRF", "Addresses");
			vty_out(vty, "%-16s%-8s%-16s%s\n", "---------",
				"------", "---", "---------");
			print_header = false; /* We have at least 1 iface */
		}
		zebra_if = ifp->info;

		vty_out(vty, "%-16s", ifp->name);

		if (if_is_up(ifp))
			vty_out(vty, "%-8s", "up");
		else
			vty_out(vty, "%-8s", "down");

		vty_out(vty, "%-16s", vrf->name);

		for (rn = route_top(zebra_if->ipv4_subnets); rn;
		     rn = route_next(rn)) {
			if (!rn->info)
				continue;
			uint32_t list_size = listcount((struct list *)rn->info);

			for (ALL_LIST_ELEMENTS_RO((struct list *)rn->info, node,
						  connected)) {
				if (!CHECK_FLAG(connected->flags,
						ZEBRA_IFA_SECONDARY)) {
					p = connected->address;
					if (first_pfx_printed) {
						/* padding to prepare row only
						 * for ip addr */
						vty_out(vty, "%-40s", "");
						if (list_size > 1)
							vty_out(vty, "+ ");
						vty_out(vty, "%pFX\n", p);
					} else {
						if (list_size > 1)
							vty_out(vty, "+ ");
						vty_out(vty, "%pFX\n", p);
					}
					first_pfx_printed = true;
					break;
				}
			}
		}

		uint32_t v6_list_size = 0;
		for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
			if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
				&& (connected->address->family == AF_INET6))
				v6_list_size++;
		}
		for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
			if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
			    && !CHECK_FLAG(connected->flags,
					   ZEBRA_IFA_SECONDARY)
			    && (connected->address->family == AF_INET6)) {
				p = connected->address;
				/* Don't print link local pfx */
				if (!IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6)) {
					if (first_pfx_printed) {
						/* padding to prepare row only
						 * for ip addr */
						vty_out(vty, "%-40s", "");
						if (v6_list_size > 1)
							vty_out(vty, "+ ");
						vty_out(vty, "%pFX\n", p);
					} else {
						if (v6_list_size > 1)
							vty_out(vty, "+ ");
						vty_out(vty, "%pFX\n", p);
					}
					first_pfx_printed = true;
					break;
				}
			}
		}
		if (!first_pfx_printed)
			vty_out(vty, "\n");
	}
	vty_out(vty, "\n");
}

static void ifs_dump_brief_vty_json(json_object *json, struct vrf *vrf)
{
	struct connected *connected;
	struct listnode *node;
	struct interface *ifp;

	FOR_ALL_INTERFACES (vrf, ifp) {
		json_object *json_if;
		json_object *json_addrs;

		json_if = json_object_new_object();
		json_object_object_add(json, ifp->name, json_if);

		json_object_string_add(json_if, "status",
				       if_is_up(ifp) ? "up" : "down");
		json_object_string_add(json_if, "vrfName", vrf->name);

		json_addrs = json_object_new_array();
		json_object_object_add(json_if, "addresses", json_addrs);
		for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
			if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
			    && !CHECK_FLAG(connected->flags,
					   ZEBRA_IFA_SECONDARY)
			    && !(connected->address->family == AF_INET6
				 && IN6_IS_ADDR_LINKLOCAL(
					 &connected->address->u.prefix6))) {
				char buf[PREFIX2STR_BUFFER];

				json_array_string_add(
					json_addrs,
					prefix2str(connected->address, buf,
						   sizeof(buf)));
			}
		}
	}
}

const char *zebra_protodown_rc_str(uint32_t protodown_rc, char *pd_buf,
				   uint32_t pd_buf_len)
{
	pd_buf[0] = '\0';
	size_t len;

	strlcat(pd_buf, "(", pd_buf_len);

	if (CHECK_FLAG(protodown_rc, ZEBRA_PROTODOWN_EXTERNAL))
		strlcat(pd_buf, "external,", pd_buf_len);

	if (CHECK_FLAG(protodown_rc, ZEBRA_PROTODOWN_EVPN_STARTUP_DELAY))
		strlcat(pd_buf, "startup-delay,", pd_buf_len);

	if (CHECK_FLAG(protodown_rc, ZEBRA_PROTODOWN_EVPN_UPLINK_DOWN))
		strlcat(pd_buf, "uplinks-down,", pd_buf_len);

	if (CHECK_FLAG(protodown_rc, ZEBRA_PROTODOWN_VRRP))
		strlcat(pd_buf, "vrrp,", pd_buf_len);

	if (CHECK_FLAG(protodown_rc, ZEBRA_PROTODOWN_SHARP))
		strlcat(pd_buf, "sharp,", pd_buf_len);

	len = strnlen(pd_buf, pd_buf_len);

	/* Remove trailing comma */
	if (pd_buf[len - 1] == ',')
		pd_buf[len - 1] = '\0';

	strlcat(pd_buf, ")", pd_buf_len);

	return pd_buf;
}

static inline bool if_is_protodown_applicable(struct interface *ifp)
{
	if (IS_ZEBRA_IF_BOND(ifp))
		return false;

	return true;
}

static void zebra_vxlan_if_vni_dump_vty(struct vty *vty,
					struct zebra_vxlan_vni *vni)
{
	char str[INET6_ADDRSTRLEN];

	vty_out(vty, "  VxLAN Id %u", vni->vni);
	if (vni->access_vlan)
		vty_out(vty, " Access VLAN Id %u\n", vni->access_vlan);

	if (vni->mcast_grp.s_addr != INADDR_ANY)
		vty_out(vty, "  Mcast Group %s",
			inet_ntop(AF_INET, &vni->mcast_grp, str, sizeof(str)));
}

static void zebra_vxlan_if_vni_hash_dump_vty(struct hash_bucket *bucket,
					     void *ctxt)
{
	struct vty *vty;
	struct zebra_vxlan_vni *vni;

	vni = (struct zebra_vxlan_vni *)bucket->data;
	vty = (struct vty *)ctxt;

	zebra_vxlan_if_vni_dump_vty(vty, vni);
}

static void zebra_vxlan_if_dump_vty(struct vty *vty, struct zebra_if *zebra_if)
{
	struct zebra_l2info_vxlan *vxlan_info;
	struct zebra_vxlan_vni_info *vni_info;

	vxlan_info = &zebra_if->l2info.vxl;
	vni_info = &vxlan_info->vni_info;

	if (vxlan_info->vtep_ip.s_addr != INADDR_ANY)
		vty_out(vty, " VTEP IP: %pI4", &vxlan_info->vtep_ip);

	if (vxlan_info->ifindex_link && (vxlan_info->link_nsid != NS_UNKNOWN)) {
		struct interface *ifp;

		ifp = if_lookup_by_index_per_ns(
			zebra_ns_lookup(vxlan_info->link_nsid),
			vxlan_info->ifindex_link);
		vty_out(vty, " Link Interface %s",
			ifp == NULL ? "Unknown" : ifp->name);
	}

	if (IS_ZEBRA_VXLAN_IF_VNI(zebra_if)) {
		zebra_vxlan_if_vni_dump_vty(vty, &vni_info->vni);
	} else {
		hash_iterate(vni_info->vni_table,
			     zebra_vxlan_if_vni_hash_dump_vty, vty);
	}

	vty_out(vty, "\n");
}

/* Interface's information print out to vty interface. */
static void if_dump_vty(struct vty *vty, struct interface *ifp)
{
	struct connected *connected;
	struct nbr_connected *nbr_connected;
	struct listnode *node;
	struct route_node *rn;
	struct zebra_if *zebra_if;
	char pd_buf[ZEBRA_PROTODOWN_RC_STR_LEN];

	zebra_if = ifp->info;

	vty_out(vty, "Interface %s is ", ifp->name);
	if (if_is_up(ifp)) {
		vty_out(vty, "up, line protocol ");

		if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
			if (if_is_running(ifp))
				vty_out(vty, "is up\n");
			else
				vty_out(vty, "is down\n");
		} else {
			vty_out(vty, "detection is disabled\n");
		}
	} else {
		vty_out(vty, "down\n");
	}

	vty_out(vty, "  Link ups:   %5u    last: %s\n", zebra_if->up_count,
		zebra_if->up_last[0] ? zebra_if->up_last : "(never)");
	vty_out(vty, "  Link downs: %5u    last: %s\n", zebra_if->down_count,
		zebra_if->down_last[0] ? zebra_if->down_last : "(never)");

	zebra_ptm_show_status(vty, NULL, ifp);

	vty_out(vty, "  vrf: %s\n", ifp->vrf->name);

	if (ifp->desc)
		vty_out(vty, "  Description: %s\n", ifp->desc);
	if (zebra_if->desc)
		vty_out(vty, "  OS Description: %s\n", zebra_if->desc);

	if (ifp->ifindex == IFINDEX_INTERNAL) {
		vty_out(vty, "  pseudo interface\n");
		return;
	} else if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		vty_out(vty, "  index %d inactive interface\n", ifp->ifindex);
		return;
	}

	vty_out(vty, "  index %d metric %d mtu %d speed %u txqlen %u",
		ifp->ifindex, ifp->metric, ifp->mtu, ifp->speed, ifp->txqlen);
	if (ifp->mtu6 != ifp->mtu)
		vty_out(vty, "mtu6 %d ", ifp->mtu6);
	vty_out(vty, "\n  flags: %s\n", if_flag_dump(ifp->flags));

	if (zebra_if->mpls)
		vty_out(vty, "  MPLS enabled\n");

	if (zebra_if->linkdown)
		vty_out(vty, "  Ignore all v4 routes with linkdown\n");
	if (zebra_if->linkdownv6)
		vty_out(vty, "  Ignore all v6 routes with linkdown\n");

	if (zebra_if->v4mcast_on)
		vty_out(vty, "  v4 Multicast forwarding is on\n");
	if (zebra_if->v6mcast_on)
		vty_out(vty, "  v6 Multicast forwarding is on\n");

	/* Hardware address. */
	vty_out(vty, "  Type: %s\n", if_link_type_str(ifp->ll_type));
	if (ifp->hw_addr_len != 0) {
		int i;

		vty_out(vty, "  HWaddr: ");
		for (i = 0; i < ifp->hw_addr_len; i++)
			vty_out(vty, "%s%02x", i == 0 ? "" : ":",
				ifp->hw_addr[i]);
		vty_out(vty, "\n");
	}

	/* Bandwidth in Mbps */
	if (ifp->bandwidth != 0) {
		vty_out(vty, "  bandwidth %u Mbps", ifp->bandwidth);
		vty_out(vty, "\n");
	}

	for (rn = route_top(zebra_if->ipv4_subnets); rn; rn = route_next(rn)) {
		if (!rn->info)
			continue;

		for (ALL_LIST_ELEMENTS_RO((struct list *)rn->info, node,
					  connected))
			connected_dump_vty(vty, NULL, connected);
	}

	for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
		if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
		    && (connected->address->family == AF_INET6))
			connected_dump_vty(vty, NULL, connected);
	}

	vty_out(vty, "  Interface Type %s\n",
		zebra_ziftype_2str(zebra_if->zif_type));
	vty_out(vty, "  Interface Slave Type %s\n",
		zebra_zifslavetype_2str(zebra_if->zif_slave_type));

	if (IS_ZEBRA_IF_BRIDGE(ifp)) {
		vty_out(vty, "  Bridge VLAN-aware: %s\n",
			IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(zebra_if) ? "yes" : "no");
	} else if (IS_ZEBRA_IF_VLAN(ifp)) {
		struct zebra_l2info_vlan *vlan_info;

		vlan_info = &zebra_if->l2info.vl;
		vty_out(vty, "  VLAN Id %u\n", vlan_info->vid);
	} else if (IS_ZEBRA_IF_VXLAN(ifp)) {
		zebra_vxlan_if_dump_vty(vty, zebra_if);
	} else if (IS_ZEBRA_IF_GRE(ifp)) {
		struct zebra_l2info_gre *gre_info;

		gre_info = &zebra_if->l2info.gre;
		if (gre_info->vtep_ip.s_addr != INADDR_ANY) {
			vty_out(vty, "  VTEP IP: %pI4", &gre_info->vtep_ip);
			if (gre_info->vtep_ip_remote.s_addr != INADDR_ANY)
				vty_out(vty, " , remote %pI4",
					&gre_info->vtep_ip_remote);
			vty_out(vty, "\n");
		}
		if (gre_info->ifindex_link &&
		    (gre_info->link_nsid != NS_UNKNOWN)) {
			struct interface *ifp;

			ifp = if_lookup_by_index_per_ns(
					zebra_ns_lookup(gre_info->link_nsid),
					gre_info->ifindex_link);
			vty_out(vty, "  Link Interface %s\n",
				ifp == NULL ? "Unknown" :
				ifp->name);
		}
	}

	if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) {
		struct zebra_l2info_brslave *br_slave;

		br_slave = &zebra_if->brslave_info;
		if (br_slave->bridge_ifindex != IFINDEX_INTERNAL) {
			if (br_slave->br_if)
				vty_out(vty, "  Master interface: %s\n",
					br_slave->br_if->name);
			else
				vty_out(vty, "  Master ifindex: %u\n",
					br_slave->bridge_ifindex);
		}
	}

	if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) {
		struct zebra_l2info_bondslave *bond_slave;

		bond_slave = &zebra_if->bondslave_info;
		if (bond_slave->bond_ifindex != IFINDEX_INTERNAL) {
			if (bond_slave->bond_if)
				vty_out(vty, "  Master interface: %s\n",
					bond_slave->bond_if->name);
			else
				vty_out(vty, "  Master ifindex: %u\n",
					bond_slave->bond_ifindex);
		}
	}

	if (zebra_if->flags & ZIF_FLAG_LACP_BYPASS)
		vty_out(vty, "  LACP bypass: on\n");

	zebra_evpn_if_es_print(vty, NULL, zebra_if);
	vty_out(vty, "  protodown: %s %s\n",
		(ZEBRA_IF_IS_PROTODOWN(zebra_if)) ? "on" : "off",
		if_is_protodown_applicable(ifp) ? "" : "(n/a)");
	if (zebra_if->protodown_rc)
		vty_out(vty, "  protodown reasons: %s\n",
			zebra_protodown_rc_str(zebra_if->protodown_rc, pd_buf,
					       sizeof(pd_buf)));

	if (zebra_if->link_ifindex != IFINDEX_INTERNAL) {
		if (zebra_if->link)
			vty_out(vty, "  Parent interface: %s\n", zebra_if->link->name);
		else
			vty_out(vty, "  Parent ifindex: %d\n", zebra_if->link_ifindex);
	}

	if (HAS_LINK_PARAMS(ifp)) {
		int i;
		struct if_link_params *iflp = ifp->link_params;
		vty_out(vty, "  Traffic Engineering Link Parameters:\n");
		if (IS_PARAM_SET(iflp, LP_TE_METRIC))
			vty_out(vty, "    TE metric %u\n", iflp->te_metric);
		if (IS_PARAM_SET(iflp, LP_MAX_BW))
			vty_out(vty, "    Maximum Bandwidth %g (Byte/s)\n",
				iflp->max_bw);
		if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW))
			vty_out(vty,
				"    Maximum Reservable Bandwidth %g (Byte/s)\n",
				iflp->max_rsv_bw);
		if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
			vty_out(vty,
				"    Unreserved Bandwidth per Class Type in Byte/s:\n");
			for (i = 0; i < MAX_CLASS_TYPE; i += 2)
				vty_out(vty,
					"      [%d]: %g (Bytes/sec),\t[%d]: %g (Bytes/sec)\n",
					i, iflp->unrsv_bw[i], i + 1,
					iflp->unrsv_bw[i + 1]);
		}

		if (IS_PARAM_SET(iflp, LP_ADM_GRP))
			vty_out(vty, "    Administrative Group:%u\n",
				iflp->admin_grp);
		if (IS_PARAM_SET(iflp, LP_DELAY)) {
			vty_out(vty, "    Link Delay Average: %u (micro-sec.)",
				iflp->av_delay);
			if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
				vty_out(vty, " Min:  %u (micro-sec.)",
					iflp->min_delay);
				vty_out(vty, " Max:  %u (micro-sec.)",
					iflp->max_delay);
			}
			vty_out(vty, "\n");
		}
		if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
			vty_out(vty,
				"    Link Delay Variation %u (micro-sec.)\n",
				iflp->delay_var);
		if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
			vty_out(vty, "    Link Packet Loss %g (in %%)\n",
				iflp->pkt_loss);
		if (IS_PARAM_SET(iflp, LP_AVA_BW))
			vty_out(vty, "    Available Bandwidth %g (Byte/s)\n",
				iflp->ava_bw);
		if (IS_PARAM_SET(iflp, LP_RES_BW))
			vty_out(vty, "    Residual Bandwidth %g (Byte/s)\n",
				iflp->res_bw);
		if (IS_PARAM_SET(iflp, LP_USE_BW))
			vty_out(vty, "    Utilized Bandwidth %g (Byte/s)\n",
				iflp->use_bw);
		if (IS_PARAM_SET(iflp, LP_RMT_AS))
			vty_out(vty, "    Neighbor ASBR IP: %pI4 AS: %u \n",
				&iflp->rmt_ip, iflp->rmt_as);
	}

	hook_call(zebra_if_extra_info, vty, ifp);

	if (listhead(ifp->nbr_connected))
		vty_out(vty, "  Neighbor address(s):\n");
	for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, nbr_connected))
		nbr_connected_dump_vty(vty, NULL, nbr_connected);

#ifdef HAVE_PROC_NET_DEV
	/* Statistics print out using proc file system. */
	vty_out(vty,
		"    %lu input packets (%lu multicast), %lu bytes, %lu dropped\n",
		ifp->stats.rx_packets, ifp->stats.rx_multicast,
		ifp->stats.rx_bytes, ifp->stats.rx_dropped);

	vty_out(vty,
		"    %lu input errors, %lu length, %lu overrun, %lu CRC, %lu frame\n",
		ifp->stats.rx_errors, ifp->stats.rx_length_errors,
		ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
		ifp->stats.rx_frame_errors);

	vty_out(vty, "    %lu fifo, %lu missed\n", ifp->stats.rx_fifo_errors,
		ifp->stats.rx_missed_errors);

	vty_out(vty, "    %lu output packets, %lu bytes, %lu dropped\n",
		ifp->stats.tx_packets, ifp->stats.tx_bytes,
		ifp->stats.tx_dropped);

	vty_out(vty,
		"    %lu output errors, %lu aborted, %lu carrier, %lu fifo, %lu heartbeat\n",
		ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
		ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
		ifp->stats.tx_heartbeat_errors);

	vty_out(vty, "    %lu window, %lu collisions\n",
		ifp->stats.tx_window_errors, ifp->stats.collisions);
#endif /* HAVE_PROC_NET_DEV */

#ifdef HAVE_NET_RT_IFLIST
	/* Statistics print out using sysctl (). */
	vty_out(vty,
		"    input packets %llu, bytes %llu, dropped %llu, multicast packets %llu\n",
		(unsigned long long)ifp->stats.ifi_ipackets,
		(unsigned long long)ifp->stats.ifi_ibytes,
		(unsigned long long)ifp->stats.ifi_iqdrops,
		(unsigned long long)ifp->stats.ifi_imcasts);

	vty_out(vty, "    input errors %llu\n",
		(unsigned long long)ifp->stats.ifi_ierrors);

	vty_out(vty,
		"    output packets %llu, bytes %llu, multicast packets %llu\n",
		(unsigned long long)ifp->stats.ifi_opackets,
		(unsigned long long)ifp->stats.ifi_obytes,
		(unsigned long long)ifp->stats.ifi_omcasts);

	vty_out(vty, "    output errors %llu\n",
		(unsigned long long)ifp->stats.ifi_oerrors);

	vty_out(vty, "    collisions %llu\n",
		(unsigned long long)ifp->stats.ifi_collisions);
#endif /* HAVE_NET_RT_IFLIST */
}

static void zebra_vxlan_if_vni_dump_vty_json(json_object *json_if,
					     struct zebra_vxlan_vni *vni)
{
	json_object_int_add(json_if, "vxlanId", vni->vni);
	if (vni->access_vlan)
		json_object_int_add(json_if, "accessVlanId", vni->access_vlan);
	if (vni->mcast_grp.s_addr != INADDR_ANY)
		json_object_string_addf(json_if, "mcastGroup", "%pI4",
					&vni->mcast_grp);
}

static void zebra_vxlan_if_vni_hash_dump_vty_json(struct hash_bucket *bucket,
						  void *ctxt)
{
	json_object *json_if;
	struct zebra_vxlan_vni *vni;

	vni = (struct zebra_vxlan_vni *)bucket->data;
	json_if = (json_object *)ctxt;

	zebra_vxlan_if_vni_dump_vty_json(json_if, vni);
}

static void zebra_vxlan_if_dump_vty_json(json_object *json_if,
					 struct zebra_if *zebra_if)
{
	struct zebra_l2info_vxlan *vxlan_info;
	struct zebra_vxlan_vni_info *vni_info;

	vxlan_info = &zebra_if->l2info.vxl;
	vni_info = &vxlan_info->vni_info;

	if (vxlan_info->vtep_ip.s_addr != INADDR_ANY)
		json_object_string_addf(json_if, "vtepIp", "%pI4",
					&vxlan_info->vtep_ip);

	if (vxlan_info->ifindex_link && (vxlan_info->link_nsid != NS_UNKNOWN)) {
		struct interface *ifp;

		ifp = if_lookup_by_index_per_ns(
			zebra_ns_lookup(vxlan_info->link_nsid),
			vxlan_info->ifindex_link);
		json_object_string_add(json_if, "linkInterface",
				       ifp == NULL ? "Unknown" : ifp->name);
	}
	if (IS_ZEBRA_VXLAN_IF_VNI(zebra_if)) {
		zebra_vxlan_if_vni_dump_vty_json(json_if, &vni_info->vni);
	} else {
		hash_iterate(vni_info->vni_table,
			     zebra_vxlan_if_vni_hash_dump_vty_json, json_if);
	}
}

static void if_dump_vty_json(struct vty *vty, struct interface *ifp,
			     json_object *json)
{
	struct connected *connected;
	struct nbr_connected *nbr_connected;
	struct listnode *node;
	struct route_node *rn;
	struct zebra_if *zebra_if;
	char pd_buf[ZEBRA_PROTODOWN_RC_STR_LEN];
	char buf[BUFSIZ];
	json_object *json_if;
	json_object *json_addrs;

	json_if = json_object_new_object();
	json_object_object_add(json, ifp->name, json_if);

	if (if_is_up(ifp)) {
		json_object_string_add(json_if, "administrativeStatus", "up");

		if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
			json_object_string_add(json_if, "operationalStatus",
					       if_is_running(ifp) ? "up"
								  : "down");
			json_object_boolean_add(json_if, "linkDetection", true);
		} else {
			json_object_boolean_add(json_if, "linkDetection",
						false);
		}
	} else {
		json_object_string_add(json_if, "administrativeStatus", "down");
	}

	zebra_if = ifp->info;

	json_object_int_add(json_if, "linkUps", zebra_if->up_count);
	json_object_int_add(json_if, "linkDowns", zebra_if->down_count);
	if (zebra_if->up_last[0])
		json_object_string_add(json_if, "lastLinkUp",
				       zebra_if->up_last);
	if (zebra_if->down_last[0])
		json_object_string_add(json_if, "lastLinkDown",
				       zebra_if->down_last);

	zebra_ptm_show_status(vty, json_if, ifp);

	json_object_string_add(json_if, "vrfName", ifp->vrf->name);

	if (ifp->desc)
		json_object_string_add(json_if, "description", ifp->desc);
	if (zebra_if->desc)
		json_object_string_add(json_if, "OsDescription",
				       zebra_if->desc);

	json_object_boolean_add(json_if, "mplsEnabled", zebra_if->mpls);
	json_object_boolean_add(json_if, "linkDown", zebra_if->linkdown);
	json_object_boolean_add(json_if, "linkDownV6", zebra_if->linkdownv6);
	json_object_boolean_add(json_if, "mcForwardingV4",
				zebra_if->v4mcast_on);
	json_object_boolean_add(json_if, "mcForwardingV6",
				zebra_if->v6mcast_on);

	if (ifp->ifindex == IFINDEX_INTERNAL) {
		json_object_boolean_add(json_if, "pseudoInterface", true);
		return;
	} else if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		json_object_int_add(json_if, "index", ifp->ifindex);
		return;
	}

	json_object_boolean_add(json_if, "pseudoInterface", false);
	json_object_int_add(json_if, "index", ifp->ifindex);
	json_object_int_add(json_if, "metric", ifp->metric);
	json_object_int_add(json_if, "mtu", ifp->mtu);
	if (ifp->mtu6 != ifp->mtu)
		json_object_int_add(json_if, "mtu6", ifp->mtu6);
	json_object_int_add(json_if, "speed", ifp->speed);
	json_object_int_add(json_if, "txqlen", ifp->txqlen);
	json_object_string_add(json_if, "flags", if_flag_dump(ifp->flags));

	/* Hardware address. */
	json_object_string_add(json_if, "type", if_link_type_str(ifp->ll_type));
	if (ifp->hw_addr_len != 0) {
		char hwbuf[BUFSIZ];

		hwbuf[0] = '\0';
		for (int i = 0; i < ifp->hw_addr_len; i++) {
			snprintf(buf, sizeof(buf), "%s%02x", i == 0 ? "" : ":",
				 ifp->hw_addr[i]);
			strlcat(hwbuf, buf, sizeof(hwbuf));
		}
		json_object_string_add(json_if, "hardwareAddress", hwbuf);
	}

	/* Bandwidth in Mbps */
	if (ifp->bandwidth != 0)
		json_object_int_add(json_if, "bandwidth", ifp->bandwidth);


	/* IP addresses. */
	json_addrs = json_object_new_array();
	json_object_object_add(json_if, "ipAddresses", json_addrs);

	for (rn = route_top(zebra_if->ipv4_subnets); rn; rn = route_next(rn)) {
		if (!rn->info)
			continue;

		for (ALL_LIST_ELEMENTS_RO((struct list *)rn->info, node,
					  connected))
			connected_dump_vty(vty, json_addrs, connected);
	}

	for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
		if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
		    && (connected->address->family == AF_INET6))
			connected_dump_vty(vty, json_addrs, connected);
	}

	json_object_string_add(json_if, "interfaceType",
			       zebra_ziftype_2str(zebra_if->zif_type));
	json_object_string_add(
		json_if, "interfaceSlaveType",
		zebra_zifslavetype_2str(zebra_if->zif_slave_type));

	if (IS_ZEBRA_IF_BRIDGE(ifp)) {
		struct zebra_l2info_bridge *bridge_info;

		bridge_info = &zebra_if->l2info.br;
		json_object_boolean_add(json_if, "bridgeVlanAware",
					bridge_info->bridge.vlan_aware);
	} else if (IS_ZEBRA_IF_VLAN(ifp)) {
		struct zebra_l2info_vlan *vlan_info;

		vlan_info = &zebra_if->l2info.vl;
		json_object_int_add(json_if, "vlanId", vlan_info->vid);
	} else if (IS_ZEBRA_IF_VXLAN(ifp)) {
		zebra_vxlan_if_dump_vty_json(json_if, zebra_if);

	} else if (IS_ZEBRA_IF_GRE(ifp)) {
		struct zebra_l2info_gre *gre_info;

		gre_info = &zebra_if->l2info.gre;
		if (gre_info->vtep_ip.s_addr != INADDR_ANY) {
			json_object_string_addf(json_if, "vtepIp", "%pI4",
						&gre_info->vtep_ip);
			if (gre_info->vtep_ip_remote.s_addr != INADDR_ANY)
				json_object_string_addf(
					json_if, "vtepRemoteIp", "%pI4",
					&gre_info->vtep_ip_remote);
		}
		if (gre_info->ifindex_link
		    && (gre_info->link_nsid != NS_UNKNOWN)) {
			struct interface *ifp;

			ifp = if_lookup_by_index_per_ns(
				zebra_ns_lookup(gre_info->link_nsid),
				gre_info->ifindex_link);
			json_object_string_add(json_if, "linkInterface",
					       ifp == NULL ? "Unknown"
							   : ifp->name);
		}
	}

	if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) {
		struct zebra_l2info_brslave *br_slave;

		br_slave = &zebra_if->brslave_info;
		if (br_slave->bridge_ifindex != IFINDEX_INTERNAL) {
			if (br_slave->br_if)
				json_object_string_add(json_if,
						       "masterInterface",
						       br_slave->br_if->name);
			else
				json_object_int_add(json_if, "masterIfindex",
						    br_slave->bridge_ifindex);
		}
	}

	if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) {
		struct zebra_l2info_bondslave *bond_slave;

		bond_slave = &zebra_if->bondslave_info;
		if (bond_slave->bond_ifindex != IFINDEX_INTERNAL) {
			if (bond_slave->bond_if)
				json_object_string_add(
					json_if, "masterInterface",
					bond_slave->bond_if->name);
			else
				json_object_int_add(json_if, "masterIfindex",
						    bond_slave->bond_ifindex);
		}
	}

	json_object_boolean_add(
		json_if, "lacpBypass",
		CHECK_FLAG(zebra_if->flags, ZIF_FLAG_LACP_BYPASS));

	zebra_evpn_if_es_print(vty, json_if, zebra_if);

	if (if_is_protodown_applicable(ifp)) {
		json_object_string_add(
			json_if, "protodown",
			(ZEBRA_IF_IS_PROTODOWN(zebra_if)) ? "on" : "off");
		if (zebra_if->protodown_rc)
			json_object_string_add(
				json_if, "protodownReason",
				zebra_protodown_rc_str(zebra_if->protodown_rc,
						       pd_buf, sizeof(pd_buf)));
	}

	if (zebra_if->link_ifindex != IFINDEX_INTERNAL) {
		if (zebra_if->link)
			json_object_string_add(json_if, "parentInterface",
					       zebra_if->link->name);
		else
			json_object_int_add(json_if, "parentIfindex",
					    zebra_if->link_ifindex);
	}

	if (HAS_LINK_PARAMS(ifp)) {
		struct if_link_params *iflp = ifp->link_params;
		json_object *json_te;

		json_te = json_object_new_object();
		json_object_object_add(
			json_if, "trafficEngineeringLinkParameters", json_te);

		if (IS_PARAM_SET(iflp, LP_TE_METRIC))
			json_object_int_add(json_te, "teMetric",
					    iflp->te_metric);
		if (IS_PARAM_SET(iflp, LP_MAX_BW))
			json_object_double_add(json_te, "maximumBandwidth",
					       iflp->max_bw);
		if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW))
			json_object_double_add(json_te,
					       "maximumReservableBandwidth",
					       iflp->max_rsv_bw);
		if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
			json_object *json_bws;

			json_bws = json_object_new_object();
			json_object_object_add(json_te, "unreservedBandwidth",
					       json_bws);
			for (unsigned int i = 0; i < MAX_CLASS_TYPE; ++i) {
				char buf_ct[64];

				snprintf(buf_ct, sizeof(buf_ct), "classType%u",
					 i);
				json_object_double_add(json_bws, buf_ct,
						       iflp->unrsv_bw[i]);
			}
		}

		if (IS_PARAM_SET(iflp, LP_ADM_GRP))
			json_object_int_add(json_te, "administrativeGroup",
					    iflp->admin_grp);
		if (IS_PARAM_SET(iflp, LP_DELAY)) {
			json_object_int_add(json_te, "linkDelayAverage",
					    iflp->av_delay);
			if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
				json_object_int_add(json_te, "linkDelayMinimum",
						    iflp->min_delay);
				json_object_int_add(json_te, "linkDelayMaximum",
						    iflp->max_delay);
			}
		}
		if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
			json_object_int_add(json_te, "linkDelayVariation",
					    iflp->delay_var);
		if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
			json_object_double_add(json_te, "linkPacketLoss",
					       iflp->pkt_loss);
		if (IS_PARAM_SET(iflp, LP_AVA_BW))
			json_object_double_add(json_te, "availableBandwidth",
					       iflp->ava_bw);
		if (IS_PARAM_SET(iflp, LP_RES_BW))
			json_object_double_add(json_te, "residualBandwidth",
					       iflp->res_bw);
		if (IS_PARAM_SET(iflp, LP_USE_BW))
			json_object_double_add(json_te, "utilizedBandwidth",
					       iflp->use_bw);
		if (IS_PARAM_SET(iflp, LP_RMT_AS))
			json_object_string_addf(json_te, "neighborAsbrIp",
						"%pI4", &iflp->rmt_ip);
		json_object_int_add(json_te, "neighborAsbrAs", iflp->rmt_as);
	}

	if (listhead(ifp->nbr_connected)) {
		json_object *json_nbr_addrs;

		json_nbr_addrs = json_object_new_array();
		json_object_object_add(json_if, "neighborIpAddresses",
				       json_nbr_addrs);

		for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
					  nbr_connected))
			nbr_connected_dump_vty(vty, json_nbr_addrs,
					       nbr_connected);
	}

#ifdef HAVE_PROC_NET_DEV
	json_object_int_add(json_if, "inputPackets", stats.rx_packets);
	json_object_int_add(json_if, "inputBytes", ifp->stats.rx_bytes);
	json_object_int_add(json_if, "inputDropped", ifp->stats.rx_dropped);
	json_object_int_add(json_if, "inputMulticastPackets",
			    ifp->stats.rx_multicast);
	json_object_int_add(json_if, "inputErrors", ifp->stats.rx_errors);
	json_object_int_add(json_if, "inputLengthErrors",
			    ifp->stats.rx_length_errors);
	json_object_int_add(json_if, "inputOverrunErrors",
			    ifp->stats.rx_over_errors);
	json_object_int_add(json_if, "inputCrcErrors",
			    ifp->stats.rx_crc_errors);
	json_object_int_add(json_if, "inputFrameErrors",
			    ifp->stats.rx_frame_errors);
	json_object_int_add(json_if, "inputFifoErrors",
			    ifp->stats.rx_fifo_errors);
	json_object_int_add(json_if, "inputMissedErrors",
			    ifp->stats.rx_missed_errors);
	json_object_int_add(json_if, "outputPackets", ifp->stats.tx_packets);
	json_object_int_add(json_if, "outputBytes", ifp->stats.tx_bytes);
	json_object_int_add(json_if, "outputDroppedPackets",
			    ifp->stats.tx_dropped);
	json_object_int_add(json_if, "outputErrors", ifp->stats.tx_errors);
	json_object_int_add(json_if, "outputAbortedErrors",
			    ifp->stats.tx_aborted_errors);
	json_object_int_add(json_if, "outputCarrierErrors",
			    ifp->stats.tx_carrier_errors);
	json_object_int_add(json_if, "outputFifoErrors",
			    ifp->stats.tx_fifo_errors);
	json_object_int_add(json_if, "outputHeartbeatErrors",
			    ifp->stats.tx_heartbeat_errors);
	json_object_int_add(json_if, "outputWindowErrors",
			    ifp->stats.tx_window_errors);
	json_object_int_add(json_if, "collisions", ifp->stats.collisions);
#endif /* HAVE_PROC_NET_DEV */

#ifdef HAVE_NET_RT_IFLIST
	json_object_int_add(json_if, "inputPackets", ifp->stats.ifi_ipackets);
	json_object_int_add(json_if, "inputBytes", ifp->stats.ifi_ibytes);
	json_object_int_add(json_if, "inputDropd", ifp->stats.ifi_iqdrops);
	json_object_int_add(json_if, "inputMulticastPackets",
			    ifp->stats.ifi_imcasts);
	json_object_int_add(json_if, "inputErrors", ifp->stats.ifi_ierrors);
	json_object_int_add(json_if, "outputPackets", ifp->stats.ifi_opackets);
	json_object_int_add(json_if, "outputBytes", ifp->stats.ifi_obytes);
	json_object_int_add(json_if, "outputMulticastPackets",
			    ifp->stats.ifi_omcasts);
	json_object_int_add(json_if, "outputErrors", ifp->stats.ifi_oerrors);
	json_object_int_add(json_if, "collisions", ifp->stats.ifi_collisions);
#endif /* HAVE_NET_RT_IFLIST */
}

static void interface_update_stats(void)
{
#ifdef HAVE_PROC_NET_DEV
	/* If system has interface statistics via proc file system, update
	   statistics. */
	ifstat_update_proc();
#endif /* HAVE_PROC_NET_DEV */
#ifdef HAVE_NET_RT_IFLIST
	ifstat_update_sysctl();
#endif /* HAVE_NET_RT_IFLIST */
}

#include "zebra/interface_clippy.c"
/* Show all interfaces to vty. */
DEFPY(show_interface, show_interface_cmd,
      "show interface vrf NAME$vrf_name [brief$brief] [json$uj]",
      SHOW_STR
      "Interface status and configuration\n"
      VRF_CMD_HELP_STR
      "Interface status and configuration summary\n"
      JSON_STR)
{
	struct vrf *vrf;
	struct interface *ifp;
	json_object *json = NULL;

	interface_update_stats();

	vrf = vrf_lookup_by_name(vrf_name);
	if (!vrf) {
		if (uj)
			vty_out(vty, "{}\n");
		else
			vty_out(vty, "%% VRF %s not found\n", vrf_name);
		return CMD_WARNING;
	}

	if (uj)
		json = json_object_new_object();

	if (brief) {
		if (json)
			ifs_dump_brief_vty_json(json, vrf);
		else
			ifs_dump_brief_vty(vty, vrf);
	} else {
		FOR_ALL_INTERFACES (vrf, ifp) {
			if (json)
				if_dump_vty_json(vty, ifp, json);
			else
				if_dump_vty(vty, ifp);
		}
	}

	if (json)
		vty_json(vty, json);

	return CMD_SUCCESS;
}


/* Show all interfaces to vty. */
DEFPY (show_interface_vrf_all,
       show_interface_vrf_all_cmd,
       "show interface [vrf all] [brief$brief] [json$uj]",
       SHOW_STR
       "Interface status and configuration\n"
       VRF_ALL_CMD_HELP_STR
       "Interface status and configuration summary\n"
       JSON_STR)
{
	struct vrf *vrf;
	struct interface *ifp;
	json_object *json = NULL;

	interface_update_stats();

	if (uj)
		json = json_object_new_object();

	/* All interface print. */
	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
		if (brief) {
			if (json)
				ifs_dump_brief_vty_json(json, vrf);
			else
				ifs_dump_brief_vty(vty, vrf);
		} else {
			FOR_ALL_INTERFACES (vrf, ifp) {
				if (json)
					if_dump_vty_json(vty, ifp, json);
				else
					if_dump_vty(vty, ifp);
			}
		}
	}

	if (json)
		vty_json(vty, json);

	return CMD_SUCCESS;
}

/* Show specified interface to vty. */

DEFPY (show_interface_name_vrf,
       show_interface_name_vrf_cmd,
       "show interface IFNAME$ifname vrf NAME$vrf_name [json$uj]",
       SHOW_STR
       "Interface status and configuration\n"
       "Interface name\n"
       VRF_CMD_HELP_STR
       JSON_STR)
{
	struct interface *ifp;
	struct vrf *vrf;
	json_object *json = NULL;

	interface_update_stats();

	vrf = vrf_lookup_by_name(vrf_name);
	if (!vrf) {
		if (uj)
			vty_out(vty, "{}\n");
		else
			vty_out(vty, "%% VRF %s not found\n", vrf_name);
		return CMD_WARNING;
	}

	ifp = if_lookup_by_name_vrf(ifname, vrf);
	if (ifp == NULL) {
		if (uj)
			vty_out(vty, "{}\n");
		else
			vty_out(vty, "%% Can't find interface %s\n", ifname);
		return CMD_WARNING;
	}

	if (uj)
		json = json_object_new_object();

	if (json)
		if_dump_vty_json(vty, ifp, json);
	else
		if_dump_vty(vty, ifp);

	if (json)
		vty_json(vty, json);

	return CMD_SUCCESS;
}

/* Show specified interface to vty. */
DEFPY (show_interface_name_vrf_all,
       show_interface_name_vrf_all_cmd,
       "show interface IFNAME$ifname [vrf all] [json$uj]",
       SHOW_STR
       "Interface status and configuration\n"
       "Interface name\n"
       VRF_ALL_CMD_HELP_STR
       JSON_STR)
{
	struct interface *ifp = NULL;
	struct interface *ifptmp;
	struct vrf *vrf;
	json_object *json = NULL;
	int count = 0;

	interface_update_stats();

	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
		ifptmp = if_lookup_by_name_vrf(ifname, vrf);
		if (ifptmp) {
			ifp = ifptmp;
			count++;
			if (!vrf_is_backend_netns())
				break;
		}
	}

	if (ifp == NULL) {
		if (uj)
			vty_out(vty, "{}\n");
		else
			vty_out(vty, "%% Can't find interface %s\n", ifname);
		return CMD_WARNING;
	}
	if (count > 1) {
		if (uj) {
			vty_out(vty, "{}\n");
		} else {
			vty_out(vty,
				"%% There are multiple interfaces with name %s\n",
				ifname);
			vty_out(vty, "%% You must specify the VRF name\n");
		}
		return CMD_WARNING;
	}

	if (uj)
		json = json_object_new_object();

	if (json)
		if_dump_vty_json(vty, ifp, json);
	else
		if_dump_vty(vty, ifp);

	if (json)
		vty_json(vty, json);

	return CMD_SUCCESS;
}

static void if_show_description(struct vty *vty, struct vrf *vrf)
{
	struct interface *ifp;

	vty_out(vty, "Interface       Status  Protocol  Description\n");
	FOR_ALL_INTERFACES (vrf, ifp) {
		int len;
		struct zebra_if *zif;
		bool intf_desc;

		intf_desc = false;

		len = vty_out(vty, "%s", ifp->name);
		vty_out(vty, "%*s", (16 - len), " ");

		if (if_is_up(ifp)) {
			vty_out(vty, "up      ");
			if (CHECK_FLAG(ifp->status,
				       ZEBRA_INTERFACE_LINKDETECTION)) {
				if (if_is_running(ifp))
					vty_out(vty, "up        ");
				else
					vty_out(vty, "down      ");
			} else {
				vty_out(vty, "unknown   ");
			}
		} else {
			vty_out(vty, "down    down      ");
		}

		if (ifp->desc) {
			intf_desc = true;
			vty_out(vty, "%s", ifp->desc);
		}
		zif = ifp->info;
		if (zif && zif->desc) {
			vty_out(vty, "%s%s",
				intf_desc
					? "\n                                  "
					: "",
				zif->desc);
		}

		vty_out(vty, "\n");
	}
}

DEFUN (show_interface_desc,
       show_interface_desc_cmd,
       "show interface description vrf NAME",
       SHOW_STR
       "Interface status and configuration\n"
       "Interface description\n"
       VRF_CMD_HELP_STR)
{
	struct vrf *vrf;

	vrf = vrf_lookup_by_name(argv[4]->arg);
	if (!vrf) {
		vty_out(vty, "%% VRF %s not found\n", argv[4]->arg);
		return CMD_WARNING;
	}

	if_show_description(vty, vrf);

	return CMD_SUCCESS;
}


DEFUN (show_interface_desc_vrf_all,
       show_interface_desc_vrf_all_cmd,
       "show interface description [vrf all]",
       SHOW_STR
       "Interface status and configuration\n"
       "Interface description\n"
       VRF_ALL_CMD_HELP_STR)
{
	struct vrf *vrf;

	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
		if (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
			vty_out(vty, "\n\tVRF %s(%u)\n\n", VRF_LOGNAME(vrf),
				vrf->vrf_id);
			if_show_description(vty, vrf);
		}

	return CMD_SUCCESS;
}

int if_multicast_set(struct interface *ifp)
{
	struct zebra_if *if_data;

	if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		if (if_set_flags(ifp, IFF_MULTICAST) < 0) {
			zlog_debug("Can't set multicast flag on interface %s",
				   ifp->name);
			return -1;
		}
		if_refresh(ifp);
	}
	if_data = ifp->info;
	if_data->multicast = IF_ZEBRA_DATA_ON;

	return 0;
}

DEFUN (multicast,
       multicast_cmd,
       "multicast",
       "Set multicast flag to interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	int ret;
	struct zebra_if *if_data;

	if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		ret = if_set_flags(ifp, IFF_MULTICAST);
		if (ret < 0) {
			vty_out(vty, "Can't set multicast flag\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
		if_refresh(ifp);
	}
	if_data = ifp->info;
	if_data->multicast = IF_ZEBRA_DATA_ON;

	return CMD_SUCCESS;
}

DEFPY (mpls,
       mpls_cmd,
       "[no] mpls <enable$on|disable$off>",
       NO_STR
       MPLS_STR
       "Set mpls to be on for the interface\n"
       "Set mpls to be off for the interface\n")
{
	if (!no)
		nb_cli_enqueue_change(vty, "./frr-zebra:zebra/mpls",
				      NB_OP_CREATE, on ? "true" : "false");
	else
		nb_cli_enqueue_change(vty, "./frr-zebra:zebra/mpls",
				      NB_OP_DESTROY, NULL);

	return nb_cli_apply_changes(vty, NULL);
}

int if_multicast_unset(struct interface *ifp)
{
	struct zebra_if *if_data;

	if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		if (if_unset_flags(ifp, IFF_MULTICAST) < 0) {
			zlog_debug("Can't unset multicast flag on interface %s",
				   ifp->name);
			return -1;
		}
		if_refresh(ifp);
	}
	if_data = ifp->info;
	if_data->multicast = IF_ZEBRA_DATA_OFF;

	return 0;
}

DEFUN (no_multicast,
       no_multicast_cmd,
       "no multicast",
       NO_STR
       "Unset multicast flag to interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	int ret;
	struct zebra_if *if_data;

	if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		ret = if_unset_flags(ifp, IFF_MULTICAST);
		if (ret < 0) {
			vty_out(vty, "Can't unset multicast flag\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
		if_refresh(ifp);
	}
	if_data = ifp->info;
	if_data->multicast = IF_ZEBRA_DATA_OFF;

	return CMD_SUCCESS;
}

int if_linkdetect(struct interface *ifp, bool detect)
{
	int if_was_operative;

	if_was_operative = if_is_no_ptm_operative(ifp);
	if (detect) {
		SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);

		/* When linkdetection is enabled, if might come down */
		if (!if_is_no_ptm_operative(ifp) && if_was_operative)
			if_down(ifp);
	} else {
		UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);

		/* Interface may come up after disabling link detection */
		if (if_is_operative(ifp) && !if_was_operative)
			if_up(ifp, true);
	}
	/* FIXME: Will defer status change forwarding if interface
	   does not come down! */
	return 0;
}

DEFUN(linkdetect, linkdetect_cmd, "link-detect",
      "Enable link detection on interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	if_linkdetect(ifp, true);

	return CMD_SUCCESS;
}


DEFUN (no_linkdetect,
       no_linkdetect_cmd,
       "no link-detect",
       NO_STR
       "Disable link detection on interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	if_linkdetect(ifp, false);

	return CMD_SUCCESS;
}

int if_shutdown(struct interface *ifp)
{
	struct zebra_if *if_data;

	if (ifp->ifindex != IFINDEX_INTERNAL) {
		/* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
		rtadv_stop_ra(ifp);
		if (if_unset_flags(ifp, IFF_UP) < 0) {
			zlog_debug("Can't shutdown interface %s", ifp->name);
			return -1;
		}
		if_refresh(ifp);
	}
	if_data = ifp->info;
	if_data->shutdown = IF_ZEBRA_DATA_ON;

	return 0;
}

DEFUN (shutdown_if,
       shutdown_if_cmd,
       "shutdown",
       "Shutdown the selected interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	int ret;
	struct zebra_if *if_data;

	if (ifp->ifindex != IFINDEX_INTERNAL) {
		/* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
		rtadv_stop_ra(ifp);
		ret = if_unset_flags(ifp, IFF_UP);
		if (ret < 0) {
			vty_out(vty, "Can't shutdown interface\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
		if_refresh(ifp);
	}
	if_data = ifp->info;
	if_data->shutdown = IF_ZEBRA_DATA_ON;

	return CMD_SUCCESS;
}

int if_no_shutdown(struct interface *ifp)
{
	struct zebra_if *if_data;

	if (ifp->ifindex != IFINDEX_INTERNAL) {
		if (if_set_flags(ifp, IFF_UP | IFF_RUNNING) < 0) {
			zlog_debug("Can't up interface %s", ifp->name);
			return -1;
		}
		if_refresh(ifp);

		/* Some addresses (in particular, IPv6 addresses on Linux) get
		 * removed when the interface goes down. They need to be
		 * readded.
		 */
		if_addr_wakeup(ifp);
	}

	if_data = ifp->info;
	if_data->shutdown = IF_ZEBRA_DATA_OFF;

	return 0;
}

DEFUN (no_shutdown_if,
       no_shutdown_if_cmd,
       "no shutdown",
       NO_STR
       "Shutdown the selected interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	int ret;
	struct zebra_if *if_data;

	if (ifp->ifindex != IFINDEX_INTERNAL) {
		ret = if_set_flags(ifp, IFF_UP | IFF_RUNNING);
		if (ret < 0) {
			vty_out(vty, "Can't up interface\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
		if_refresh(ifp);

		/* Some addresses (in particular, IPv6 addresses on Linux) get
		 * removed when the interface goes down. They need to be
		 * readded.
		 */
		if_addr_wakeup(ifp);
	}

	if_data = ifp->info;
	if_data->shutdown = IF_ZEBRA_DATA_OFF;

	return CMD_SUCCESS;
}

DEFUN (bandwidth_if,
       bandwidth_if_cmd,
       "bandwidth (1-100000)",
       "Set bandwidth informational parameter\n"
       "Bandwidth in megabits\n")
{
	int idx_number = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	unsigned int bandwidth;

	bandwidth = strtol(argv[idx_number]->arg, NULL, 10);

	/* bandwidth range is <1-100000> */
	if (bandwidth < 1 || bandwidth > 100000) {
		vty_out(vty, "Bandwidth is invalid\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	ifp->bandwidth = bandwidth;

	/* force protocols to recalculate routes due to cost change */
	if (if_is_operative(ifp))
		zebra_interface_up_update(ifp);

	return CMD_SUCCESS;
}

DEFUN (no_bandwidth_if,
       no_bandwidth_if_cmd,
       "no bandwidth [(1-100000)]",
       NO_STR
       "Set bandwidth informational parameter\n"
       "Bandwidth in megabits\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	ifp->bandwidth = 0;

	/* force protocols to recalculate routes due to cost change */
	if (if_is_operative(ifp))
		zebra_interface_up_update(ifp);

	return CMD_SUCCESS;
}


struct cmd_node link_params_node = {
	.name = "link-params",
	.node = LINK_PARAMS_NODE,
	.parent_node = INTERFACE_NODE,
	.prompt = "%s(config-link-params)# ",
	.no_xpath = true,
};

static void link_param_cmd_set_uint32(struct interface *ifp, uint32_t *field,
				      uint32_t type, uint32_t value)
{
	/* Update field as needed */
	if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value) {
		*field = value;
		SET_PARAM(ifp->link_params, type);

		/* force protocols to update LINK STATE due to parameters change
		 */
		if (if_is_operative(ifp))
			zebra_interface_parameters_update(ifp);
	}
}
static void link_param_cmd_set_float(struct interface *ifp, float *field,
				     uint32_t type, float value)
{

	/* Update field as needed */
	if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value) {
		*field = value;
		SET_PARAM(ifp->link_params, type);

		/* force protocols to update LINK STATE due to parameters change
		 */
		if (if_is_operative(ifp))
			zebra_interface_parameters_update(ifp);
	}
}

static void link_param_cmd_unset(struct interface *ifp, uint32_t type)
{
	if (ifp->link_params == NULL)
		return;

	/* Unset field */
	UNSET_PARAM(ifp->link_params, type);

	/* force protocols to update LINK STATE due to parameters change */
	if (if_is_operative(ifp))
		zebra_interface_parameters_update(ifp);
}

DEFUN_NOSH (link_params,
       link_params_cmd,
       "link-params",
       LINK_PARAMS_STR)
{
	/* vty->qobj_index stays the same @ interface pointer */
	vty->node = LINK_PARAMS_NODE;

	return CMD_SUCCESS;
}

DEFUN_NOSH (exit_link_params,
       exit_link_params_cmd,
       "exit-link-params",
       "Exit from Link Params configuration mode\n")
{
	if (vty->node == LINK_PARAMS_NODE)
		vty->node = INTERFACE_NODE;
	return CMD_SUCCESS;
}

/* Specific Traffic Engineering parameters commands */
DEFUN (link_params_enable,
       link_params_enable_cmd,
       "enable",
       "Activate link parameters on this interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	/* This command could be issue at startup, when activate MPLS TE */
	/* on a new interface or after a ON / OFF / ON toggle */
	/* In all case, TE parameters are reset to their default factory */
	if (IS_ZEBRA_DEBUG_EVENT || IS_ZEBRA_DEBUG_MPLS)
		zlog_debug(
			"Link-params: enable TE link parameters on interface %s",
			ifp->name);

	if (!if_link_params_get(ifp))
		if_link_params_enable(ifp);

	/* force protocols to update LINK STATE due to parameters change */
	if (if_is_operative(ifp))
		zebra_interface_parameters_update(ifp);

	return CMD_SUCCESS;
}

DEFUN (no_link_params_enable,
       no_link_params_enable_cmd,
       "no enable",
       NO_STR
       "Disable link parameters on this interface\n")
{
	char xpath[XPATH_MAXLEN];
	int ret;
	VTY_DECLVAR_CONTEXT(interface, ifp);

	if (IS_ZEBRA_DEBUG_EVENT || IS_ZEBRA_DEBUG_MPLS)
		zlog_debug("MPLS-TE: disable TE link parameters on interface %s",
			   ifp->name);

	if_link_params_free(ifp);

	snprintf(
		xpath, sizeof(xpath),
		"/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities",
		ifp->name);
	if (yang_dnode_exists(running_config->dnode, xpath))
		nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);

	ret = nb_cli_apply_changes(vty, NULL);

	if (ret != CMD_SUCCESS)
		return ret;

	/* force protocols to update LINK STATE due to parameters change */
	if (if_is_operative(ifp))
		zebra_interface_parameters_update(ifp);

	return CMD_SUCCESS;
}

/* STANDARD TE metrics */
DEFUN (link_params_metric,
       link_params_metric_cmd,
       "metric (0-4294967295)",
       "Link metric for MPLS-TE purpose\n"
       "Metric value in decimal\n")
{
	int idx_number = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	uint32_t metric;

	metric = strtoul(argv[idx_number]->arg, NULL, 10);

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update TE metric if needed */
	link_param_cmd_set_uint32(ifp, &iflp->te_metric, LP_TE_METRIC, metric);

	return CMD_SUCCESS;
}

DEFUN (no_link_params_metric,
       no_link_params_metric_cmd,
       "no metric",
       NO_STR
       "Disable Link Metric on this interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	/* Unset TE Metric */
	link_param_cmd_unset(ifp, LP_TE_METRIC);

	return CMD_SUCCESS;
}

DEFUN (link_params_maxbw,
       link_params_maxbw_cmd,
       "max-bw BANDWIDTH",
       "Maximum bandwidth that can be used\n"
       "Bytes/second (IEEE floating point format)\n")
{
	int idx_bandwidth = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);

	float bw;

	if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
		vty_out(vty, "link_params_maxbw: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check that Maximum bandwidth is not lower than other bandwidth
	 * parameters */
	if (iflp && ((bw <= iflp->max_rsv_bw) || (bw <= iflp->unrsv_bw[0]) ||
		     (bw <= iflp->unrsv_bw[1]) || (bw <= iflp->unrsv_bw[2]) ||
		     (bw <= iflp->unrsv_bw[3]) || (bw <= iflp->unrsv_bw[4]) ||
		     (bw <= iflp->unrsv_bw[5]) || (bw <= iflp->unrsv_bw[6]) ||
		     (bw <= iflp->unrsv_bw[7]) || (bw <= iflp->ava_bw) ||
		     (bw <= iflp->res_bw) || (bw <= iflp->use_bw))) {
		vty_out(vty,
			"Maximum Bandwidth could not be lower than others bandwidth\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update Maximum Bandwidth if needed */
	link_param_cmd_set_float(ifp, &iflp->max_bw, LP_MAX_BW, bw);

	return CMD_SUCCESS;
}

DEFUN (link_params_max_rsv_bw,
       link_params_max_rsv_bw_cmd,
       "max-rsv-bw BANDWIDTH",
       "Maximum bandwidth that may be reserved\n"
       "Bytes/second (IEEE floating point format)\n")
{
	int idx_bandwidth = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	float bw;

	if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
		vty_out(vty, "link_params_max_rsv_bw: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check that bandwidth is not greater than maximum bandwidth parameter
	 */
	if (iflp && bw > iflp->max_bw) {
		vty_out(vty,
			"Maximum Reservable Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
			iflp->max_bw);
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update Maximum Reservable Bandwidth if needed */
	link_param_cmd_set_float(ifp, &iflp->max_rsv_bw, LP_MAX_RSV_BW, bw);

	return CMD_SUCCESS;
}

DEFUN (link_params_unrsv_bw,
       link_params_unrsv_bw_cmd,
       "unrsv-bw (0-7) BANDWIDTH",
       "Unreserved bandwidth at each priority level\n"
       "Priority\n"
       "Bytes/second (IEEE floating point format)\n")
{
	int idx_number = 1;
	int idx_bandwidth = 2;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	int priority;
	float bw;

	/* We don't have to consider about range check here. */
	if (sscanf(argv[idx_number]->arg, "%d", &priority) != 1) {
		vty_out(vty, "link_params_unrsv_bw: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
		vty_out(vty, "link_params_unrsv_bw: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check that bandwidth is not greater than maximum bandwidth parameter
	 */
	if (iflp && bw > iflp->max_bw) {
		vty_out(vty,
			"UnReserved Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
			iflp->max_bw);
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update Unreserved Bandwidth if needed */
	link_param_cmd_set_float(ifp, &iflp->unrsv_bw[priority], LP_UNRSV_BW,
				 bw);

	return CMD_SUCCESS;
}

DEFPY_YANG(link_params_admin_grp, link_params_admin_grp_cmd,
	   "admin-grp BITPATTERN",
	   "Administrative group membership\n"
	   "32-bit Hexadecimal value (e.g. 0xa1)\n")
{
	char xpath[XPATH_MAXLEN];
	int idx_bitpattern = 1;
	unsigned long value;
	char value_str[11];

	VTY_DECLVAR_CONTEXT(interface, ifp);

	snprintf(
		xpath, sizeof(xpath),
		"/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities",
		ifp->name);
	if (yang_dnode_exists(running_config->dnode, xpath)) {
		vty_out(vty,
			"cannot use the admin-grp command when affinity is set\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (sscanf(argv[idx_bitpattern]->arg, "0x%lx", &value) != 1) {
		vty_out(vty, "link_params_admin_grp: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (value > 0xFFFFFFFF) {
		vty_out(vty, "value must be not be superior to 0xFFFFFFFF\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	snprintf(value_str, sizeof(value_str), "%ld", value);

	nb_cli_enqueue_change(
		vty, "./frr-zebra:zebra/link-params/legacy-admin-group",
		NB_OP_MODIFY, value_str);

	return nb_cli_apply_changes(vty, NULL);
}

DEFPY_YANG(no_link_params_admin_grp, no_link_params_admin_grp_cmd,
	   "no admin-grp",
	   NO_STR "Disable Administrative group membership on this interface\n")
{
	nb_cli_enqueue_change(
		vty, "./frr-zebra:zebra/link-params/legacy-admin-group",
		NB_OP_DESTROY, NULL);

	return nb_cli_apply_changes(vty, NULL);
}

/* RFC5392 & RFC5316: INTER-AS */
DEFUN (link_params_inter_as,
       link_params_inter_as_cmd,
       "neighbor A.B.C.D as (1-4294967295)",
       "Configure remote ASBR information (Neighbor IP address and AS number)\n"
       "Remote IP address in dot decimal A.B.C.D\n"
       "Remote AS number\n"
       "AS number in the range <1-4294967295>\n")
{
	int idx_ipv4 = 1;
	int idx_number = 3;

	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	struct in_addr addr;
	uint32_t as;

	if (!inet_aton(argv[idx_ipv4]->arg, &addr)) {
		vty_out(vty, "Please specify Router-Addr by A.B.C.D\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	as = strtoul(argv[idx_number]->arg, NULL, 10);

	/* Update Remote IP and Remote AS fields if needed */
	if (IS_PARAM_UNSET(iflp, LP_RMT_AS) || iflp->rmt_as != as
	    || iflp->rmt_ip.s_addr != addr.s_addr) {

		iflp->rmt_as = as;
		iflp->rmt_ip.s_addr = addr.s_addr;
		SET_PARAM(iflp, LP_RMT_AS);

		/* force protocols to update LINK STATE due to parameters change
		 */
		if (if_is_operative(ifp))
			zebra_interface_parameters_update(ifp);
	}
	return CMD_SUCCESS;
}

DEFUN (no_link_params_inter_as,
       no_link_params_inter_as_cmd,
       "no neighbor",
       NO_STR
       "Remove Neighbor IP address and AS number for Inter-AS TE\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);

	if (!iflp)
		return CMD_SUCCESS;

	/* Reset Remote IP and AS neighbor */
	iflp->rmt_as = 0;
	iflp->rmt_ip.s_addr = 0;
	UNSET_PARAM(iflp, LP_RMT_AS);

	/* force protocols to update LINK STATE due to parameters change */
	if (if_is_operative(ifp))
		zebra_interface_parameters_update(ifp);

	return CMD_SUCCESS;
}

/* RFC7471: OSPF Traffic Engineering (TE) Metric extensions &
 * draft-ietf-isis-metric-extensions-07.txt */
DEFUN (link_params_delay,
       link_params_delay_cmd,
       "delay (0-16777215) [min (0-16777215) max (0-16777215)]",
       "Unidirectional Average Link Delay\n"
       "Average delay in micro-second as decimal (0...16777215)\n"
       "Minimum delay\n"
       "Minimum delay in micro-second as decimal (0...16777215)\n"
       "Maximum delay\n"
       "Maximum delay in micro-second as decimal (0...16777215)\n")
{
	/* Get and Check new delay values */
	uint32_t delay = 0, low = 0, high = 0;
	delay = strtoul(argv[1]->arg, NULL, 10);
	if (argc == 6) {
		low = strtoul(argv[3]->arg, NULL, 10);
		high = strtoul(argv[5]->arg, NULL, 10);
	}

	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	uint8_t update = 0;

	if (argc == 2) {
		/*
		 * Check new delay value against old Min and Max delays if set
		 *
		 * RFC 7471 Section 4.2.7:
		 *    It is possible for min delay and max delay to be
		 *    the same value.
		 *
		 * Therefore, it is also allowed that the average
		 * delay be equal to the min delay or max delay.
		 */
		if (iflp && IS_PARAM_SET(iflp, LP_MM_DELAY) &&
		    (delay < iflp->min_delay || delay > iflp->max_delay)) {
			vty_out(vty,
				"Average delay should be in range Min (%d) - Max (%d) delay\n",
				iflp->min_delay, iflp->max_delay);
			return CMD_WARNING_CONFIG_FAILED;
		}

		if (!iflp)
			iflp = if_link_params_enable(ifp);

		/* Update delay if value is not set or change */
		if (IS_PARAM_UNSET(iflp, LP_DELAY) || iflp->av_delay != delay) {
			iflp->av_delay = delay;
			SET_PARAM(iflp, LP_DELAY);
			update = 1;
		}
		/* Unset Min and Max delays if already set */
		if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
			iflp->min_delay = 0;
			iflp->max_delay = 0;
			UNSET_PARAM(iflp, LP_MM_DELAY);
			update = 1;
		}
	} else {
		/*
		 * Check new delays value coherency. See above note
		 * regarding average delay equal to min/max allowed
		 */
		if (delay < low || delay > high) {
			vty_out(vty,
				"Average delay should be in range Min (%d) - Max (%d) delay\n",
				low, high);
			return CMD_WARNING_CONFIG_FAILED;
		}

		if (!iflp)
			iflp = if_link_params_enable(ifp);

		/* Update Delays if needed */
		if (IS_PARAM_UNSET(iflp, LP_DELAY)
		    || IS_PARAM_UNSET(iflp, LP_MM_DELAY)
		    || iflp->av_delay != delay || iflp->min_delay != low
		    || iflp->max_delay != high) {
			iflp->av_delay = delay;
			SET_PARAM(iflp, LP_DELAY);
			iflp->min_delay = low;
			iflp->max_delay = high;
			SET_PARAM(iflp, LP_MM_DELAY);
			update = 1;
		}
	}

	/* force protocols to update LINK STATE due to parameters change */
	if (update == 1 && if_is_operative(ifp))
		zebra_interface_parameters_update(ifp);

	return CMD_SUCCESS;
}

DEFUN (no_link_params_delay,
       no_link_params_delay_cmd,
       "no delay",
       NO_STR
       "Disable Unidirectional Average, Min & Max Link Delay on this interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);

	if (!iflp)
		return CMD_SUCCESS;

	/* Unset Delays */
	iflp->av_delay = 0;
	UNSET_PARAM(iflp, LP_DELAY);
	iflp->min_delay = 0;
	iflp->max_delay = 0;
	UNSET_PARAM(iflp, LP_MM_DELAY);

	/* force protocols to update LINK STATE due to parameters change */
	if (if_is_operative(ifp))
		zebra_interface_parameters_update(ifp);

	return CMD_SUCCESS;
}

DEFUN (link_params_delay_var,
       link_params_delay_var_cmd,
       "delay-variation (0-16777215)",
       "Unidirectional Link Delay Variation\n"
       "delay variation in micro-second as decimal (0...16777215)\n")
{
	int idx_number = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	uint32_t value;

	value = strtoul(argv[idx_number]->arg, NULL, 10);

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update Delay Variation if needed */
	link_param_cmd_set_uint32(ifp, &iflp->delay_var, LP_DELAY_VAR, value);

	return CMD_SUCCESS;
}

DEFUN (no_link_params_delay_var,
       no_link_params_delay_var_cmd,
       "no delay-variation",
       NO_STR
       "Disable Unidirectional Delay Variation on this interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	/* Unset Delay Variation */
	link_param_cmd_unset(ifp, LP_DELAY_VAR);

	return CMD_SUCCESS;
}

DEFUN (link_params_pkt_loss,
       link_params_pkt_loss_cmd,
       "packet-loss PERCENTAGE",
       "Unidirectional Link Packet Loss\n"
       "percentage of total traffic by 0.000003% step and less than 50.331642%\n")
{
	int idx_percentage = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	float fval;

	if (sscanf(argv[idx_percentage]->arg, "%g", &fval) != 1) {
		vty_out(vty, "link_params_pkt_loss: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (fval > MAX_PKT_LOSS)
		fval = MAX_PKT_LOSS;

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update Packet Loss if needed */
	link_param_cmd_set_float(ifp, &iflp->pkt_loss, LP_PKT_LOSS, fval);

	return CMD_SUCCESS;
}

DEFUN (no_link_params_pkt_loss,
       no_link_params_pkt_loss_cmd,
       "no packet-loss",
       NO_STR
       "Disable Unidirectional Link Packet Loss on this interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	/* Unset Packet Loss */
	link_param_cmd_unset(ifp, LP_PKT_LOSS);

	return CMD_SUCCESS;
}

DEFUN (link_params_res_bw,
       link_params_res_bw_cmd,
       "res-bw BANDWIDTH",
       "Unidirectional Residual Bandwidth\n"
       "Bytes/second (IEEE floating point format)\n")
{
	int idx_bandwidth = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	float bw;

	if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
		vty_out(vty, "link_params_res_bw: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check that bandwidth is not greater than maximum bandwidth parameter
	 */
	if (iflp && bw > iflp->max_bw) {
		vty_out(vty,
			"Residual Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
			iflp->max_bw);
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update Residual Bandwidth if needed */
	link_param_cmd_set_float(ifp, &iflp->res_bw, LP_RES_BW, bw);

	return CMD_SUCCESS;
}

DEFUN (no_link_params_res_bw,
       no_link_params_res_bw_cmd,
       "no res-bw",
       NO_STR
       "Disable Unidirectional Residual Bandwidth on this interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	/* Unset Residual Bandwidth */
	link_param_cmd_unset(ifp, LP_RES_BW);

	return CMD_SUCCESS;
}

DEFUN (link_params_ava_bw,
       link_params_ava_bw_cmd,
       "ava-bw BANDWIDTH",
       "Unidirectional Available Bandwidth\n"
       "Bytes/second (IEEE floating point format)\n")
{
	int idx_bandwidth = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	float bw;

	if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
		vty_out(vty, "link_params_ava_bw: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check that bandwidth is not greater than maximum bandwidth parameter
	 */
	if (iflp && bw > iflp->max_bw) {
		vty_out(vty,
			"Available Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
			iflp->max_bw);
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update Residual Bandwidth if needed */
	link_param_cmd_set_float(ifp, &iflp->ava_bw, LP_AVA_BW, bw);

	return CMD_SUCCESS;
}

DEFUN (no_link_params_ava_bw,
       no_link_params_ava_bw_cmd,
       "no ava-bw",
       NO_STR
       "Disable Unidirectional Available Bandwidth on this interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	/* Unset Available Bandwidth */
	link_param_cmd_unset(ifp, LP_AVA_BW);

	return CMD_SUCCESS;
}

DEFUN (link_params_use_bw,
       link_params_use_bw_cmd,
       "use-bw BANDWIDTH",
       "Unidirectional Utilised Bandwidth\n"
       "Bytes/second (IEEE floating point format)\n")
{
	int idx_bandwidth = 1;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct if_link_params *iflp = if_link_params_get(ifp);
	float bw;

	if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
		vty_out(vty, "link_params_use_bw: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check that bandwidth is not greater than maximum bandwidth parameter
	 */
	if (iflp && bw > iflp->max_bw) {
		vty_out(vty,
			"Utilised Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
			iflp->max_bw);
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (!iflp)
		iflp = if_link_params_enable(ifp);

	/* Update Utilized Bandwidth if needed */
	link_param_cmd_set_float(ifp, &iflp->use_bw, LP_USE_BW, bw);

	return CMD_SUCCESS;
}

DEFUN (no_link_params_use_bw,
       no_link_params_use_bw_cmd,
       "no use-bw",
       NO_STR
       "Disable Unidirectional Utilised Bandwidth on this interface\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);

	/* Unset Utilised Bandwidth */
	link_param_cmd_unset(ifp, LP_USE_BW);

	return CMD_SUCCESS;
}

static int ag_change(struct vty *vty, int argc, struct cmd_token **argv,
		     const char *xpath, bool no, int start_idx)
{
	for (int i = start_idx; i < argc; i++)
		nb_cli_enqueue_change(vty, xpath,
				      no ? NB_OP_DESTROY : NB_OP_CREATE,
				      argv[i]->arg);
	return nb_cli_apply_changes(vty, NULL);
}

/*
 * XPath:
 * /frr-interface:lib/interface/frr-zebra:zebra/link-params/affinities/affinity
 */
DEFPY_YANG(link_params_affinity, link_params_affinity_cmd,
	   "[no] affinity NAME...",
	   NO_STR
	   "Interface affinities\n"
	   "Affinity names\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	char xpath[XPATH_MAXLEN];

	snprintf(
		xpath, sizeof(xpath),
		"/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/legacy-admin-group",
		ifp->name);
	if (yang_dnode_exists(running_config->dnode, xpath)) {
		vty_out(vty,
			"cannot use the affinity command when admin-grp is set\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	return ag_change(vty, argc, argv,
			 "./frr-zebra:zebra/link-params/affinities/affinity",
			 no, no ? 2 : 1);
}


/*
 * XPath:
 * /frr-interface:lib/interface/frr-zebra:zebra/link-params/affinities/affinity-mode
 */
DEFPY_YANG(link_params_affinity_mode, link_params_affinity_mode_cmd,
	   "affinity-mode <standard|extended|both>$affmode",
	   "Interface affinity mode\n"
	   "Standard Admin-Group only RFC3630,5305,5329 (default)\n"
	   "Extended Admin-Group only RFC7308\n"
	   "Standard and extended Admin-Group format\n")
{
	const char *xpath = "./frr-zebra:zebra/link-params/affinity-mode";

	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, affmode);

	return nb_cli_apply_changes(vty, NULL);
}

DEFPY_YANG(no_link_params_affinity_mode, no_link_params_affinity_mode_cmd,
	   "no affinity-mode [<standard|extended|both>]",
	   NO_STR
	   "Interface affinity mode\n"
	   "Standard Admin-Group only RFC3630,5305,5329 (default)\n"
	   "Extended Admin-Group only RFC7308\n"
	   "Standard and extended Admin-Group format\n")
{
	const char *xpath = "./frr-zebra:zebra/link-params/affinity-mode";

	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, "standard");

	return nb_cli_apply_changes(vty, NULL);
}

static int ag_iter_cb(const struct lyd_node *dnode, void *arg)
{
	struct vty *vty = (struct vty *)arg;

	vty_out(vty, " %s", yang_dnode_get_string(dnode, "."));
	return YANG_ITER_CONTINUE;
}

void cli_show_legacy_admin_group(struct vty *vty, const struct lyd_node *dnode,
				 bool show_defaults)
{
	if (!yang_dnode_exists(dnode, "./legacy-admin-group"))
		return;

	vty_out(vty, "  admin-group 0x%x\n",
		yang_dnode_get_uint32(dnode, "./legacy-admin-group"));
}

void cli_show_affinity_mode(struct vty *vty, const struct lyd_node *dnode,
			    bool show_defaults)
{
	enum affinity_mode affinity_mode = yang_dnode_get_enum(dnode, ".");

	if (affinity_mode == AFFINITY_MODE_STANDARD)
		vty_out(vty, "  affinity-mode standard\n");
	else if (affinity_mode == AFFINITY_MODE_BOTH)
		vty_out(vty, "  affinity-mode both\n");
}

void cli_show_affinity(struct vty *vty, const struct lyd_node *dnode,
		       bool show_defaults)
{
	if (!yang_dnode_exists(dnode, "./affinity"))
		return;

	vty_out(vty, "  affinity");
	yang_dnode_iterate(ag_iter_cb, vty, dnode, "./affinity");
	vty_out(vty, "\n");
}

int if_ip_address_install(struct interface *ifp, struct prefix *prefix,
			  const char *label, struct prefix *pp)
{
	struct zebra_if *if_data;
	struct prefix_ipv4 lp;
	struct prefix_ipv4 *p;
	struct connected *ifc;
	enum zebra_dplane_result dplane_res;

	if_data = ifp->info;

	lp.family = prefix->family;
	lp.prefix = prefix->u.prefix4;
	lp.prefixlen = prefix->prefixlen;
	apply_mask_ipv4(&lp);

	ifc = connected_check_ptp(ifp, &lp, pp ? pp : NULL);
	if (!ifc) {
		ifc = connected_new();
		ifc->ifp = ifp;

		/* Address. */
		p = prefix_ipv4_new();
		*p = lp;
		ifc->address = (struct prefix *)p;

		if (pp) {
			SET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
			p = prefix_ipv4_new();
			*p = *(struct prefix_ipv4 *)pp;
			ifc->destination = (struct prefix *)p;
		}

		/* Label. */
		if (label)
			ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);

		/* Add to linked list. */
		listnode_add(ifp->connected, ifc);
	}

	/* This address is configured from zebra. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
		SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);

	/* In case of this route need to install kernel. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED) &&
	    CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE) &&
	    !(if_data && if_data->shutdown == IF_ZEBRA_DATA_ON)) {
		/* Some system need to up the interface to set IP address. */
		if (!if_is_up(ifp)) {
			if_set_flags(ifp, IFF_UP | IFF_RUNNING);
			if_refresh(ifp);
		}

		dplane_res = dplane_intf_addr_set(ifp, ifc);
		if (dplane_res == ZEBRA_DPLANE_REQUEST_FAILURE) {
			zlog_debug(
				"dplane can't set interface IP address: %s.",
				dplane_res2str(dplane_res));
			return NB_ERR;
		}

		SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
		/* The address will be advertised to zebra clients when the
		 * notification
		 * from the kernel has been received.
		 * It will also be added to the subnet chain list, then. */
	}

	return 0;
}

static int ip_address_install(struct vty *vty, struct interface *ifp,
			      const char *addr_str, const char *peer_str,
			      const char *label)
{
	struct zebra_if *if_data;
	struct prefix_ipv4 lp, pp;
	struct connected *ifc;
	struct prefix_ipv4 *p;
	int ret;
	enum zebra_dplane_result dplane_res;

	if_data = ifp->info;

	ret = str2prefix_ipv4(addr_str, &lp);
	if (ret <= 0) {
		vty_out(vty, "%% Malformed address \n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (ipv4_martian(&lp.prefix)) {
		vty_out(vty, "%% Invalid address\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (peer_str) {
		if (lp.prefixlen != IPV4_MAX_BITLEN) {
			vty_out(vty,
				"%% Local prefix length for P-t-P address must be /32\n");
			return CMD_WARNING_CONFIG_FAILED;
		}

		ret = str2prefix_ipv4(peer_str, &pp);
		if (ret <= 0) {
			vty_out(vty, "%% Malformed peer address\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
	}

	ifc = connected_check_ptp(ifp, &lp, peer_str ? &pp : NULL);
	if (!ifc) {
		ifc = connected_new();
		ifc->ifp = ifp;

		/* Address. */
		p = prefix_ipv4_new();
		*p = lp;
		ifc->address = (struct prefix *)p;

		if (peer_str) {
			SET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
			p = prefix_ipv4_new();
			*p = pp;
			ifc->destination = (struct prefix *)p;
		}

		/* Label. */
		if (label)
			ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);

		/* Add to linked list. */
		listnode_add(ifp->connected, ifc);
	}

	/* This address is configured from zebra. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
		SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);

	/* In case of this route need to install kernel. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED) &&
	    CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE) &&
	    !(if_data && if_data->shutdown == IF_ZEBRA_DATA_ON)) {
		/* Some system need to up the interface to set IP address. */
		if (!if_is_up(ifp)) {
			if_set_flags(ifp, IFF_UP | IFF_RUNNING);
			if_refresh(ifp);
		}

		dplane_res = dplane_intf_addr_set(ifp, ifc);
		if (dplane_res == ZEBRA_DPLANE_REQUEST_FAILURE) {
			vty_out(vty, "%% Can't set interface IP address: %s.\n",
				dplane_res2str(dplane_res));
			return CMD_WARNING_CONFIG_FAILED;
		}

		SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
		/* The address will be advertised to zebra clients when the
		 * notification
		 * from the kernel has been received.
		 * It will also be added to the subnet chain list, then. */
	}

	return CMD_SUCCESS;
}

int if_ip_address_uinstall(struct interface *ifp, struct prefix *prefix)
{
	struct connected *ifc = NULL;
	enum zebra_dplane_result dplane_res;

	if (prefix->family == AF_INET) {
		/* Check current interface address. */
		ifc = connected_check_ptp(ifp, prefix, NULL);
		if (!ifc) {
			zlog_debug("interface %s Can't find address",
				   ifp->name);
			return -1;
		}

	} else if (prefix->family == AF_INET6) {
		/* Check current interface address. */
		ifc = connected_check(ifp, prefix);
	}

	if (!ifc) {
		zlog_debug("interface %s Can't find address", ifp->name);
		return -1;
	}
	UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);

	/* This is not real address or interface is not active. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
	    || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		listnode_delete(ifp->connected, ifc);
		connected_free(&ifc);
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* This is real route. */
	dplane_res = dplane_intf_addr_unset(ifp, ifc);
	if (dplane_res == ZEBRA_DPLANE_REQUEST_FAILURE) {
		zlog_debug("Can't unset interface IP address: %s.",
			   dplane_res2str(dplane_res));
		return -1;
	}
	UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);

	return 0;
}

static int ip_address_uninstall(struct vty *vty, struct interface *ifp,
				const char *addr_str, const char *peer_str,
				const char *label)
{
	struct prefix_ipv4 lp, pp;
	struct connected *ifc;
	int ret;
	enum zebra_dplane_result dplane_res;

	/* Convert to prefix structure. */
	ret = str2prefix_ipv4(addr_str, &lp);
	if (ret <= 0) {
		vty_out(vty, "%% Malformed address \n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (peer_str) {
		if (lp.prefixlen != IPV4_MAX_BITLEN) {
			vty_out(vty,
				"%% Local prefix length for P-t-P address must be /32\n");
			return CMD_WARNING_CONFIG_FAILED;
		}

		ret = str2prefix_ipv4(peer_str, &pp);
		if (ret <= 0) {
			vty_out(vty, "%% Malformed peer address\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
	}

	/* Check current interface address. */
	ifc = connected_check_ptp(ifp, &lp, peer_str ? &pp : NULL);
	if (!ifc) {
		vty_out(vty, "%% Can't find address\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* This is not configured address. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
		return CMD_WARNING_CONFIG_FAILED;

	UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);

	/* This is not real address or interface is not active. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
	    || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		listnode_delete(ifp->connected, ifc);
		connected_free(&ifc);
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* This is real route. */
	dplane_res = dplane_intf_addr_unset(ifp, ifc);
	if (dplane_res == ZEBRA_DPLANE_REQUEST_FAILURE) {
		vty_out(vty, "%% Can't unset interface IP address: %s.\n",
			dplane_res2str(dplane_res));
		return CMD_WARNING_CONFIG_FAILED;
	}
	UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
	/* we will receive a kernel notification about this route being removed.
	 * this will trigger its removal from the connected list. */
	return CMD_SUCCESS;
}

DEFUN (ip_address,
       ip_address_cmd,
       "ip address A.B.C.D/M",
       "Interface Internet Protocol config commands\n"
       "Set the IP address of an interface\n"
       "IP address (e.g. 10.0.0.1/8)\n")
{
	int idx_ipv4_prefixlen = 2;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	return ip_address_install(vty, ifp, argv[idx_ipv4_prefixlen]->arg, NULL,
				  NULL);
}

DEFUN (no_ip_address,
       no_ip_address_cmd,
       "no ip address A.B.C.D/M",
       NO_STR
       "Interface Internet Protocol config commands\n"
       "Set the IP address of an interface\n"
       "IP Address (e.g. 10.0.0.1/8)\n")
{
	int idx_ipv4_prefixlen = 3;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	return ip_address_uninstall(vty, ifp, argv[idx_ipv4_prefixlen]->arg,
				    NULL, NULL);
}

DEFUN(ip_address_peer,
      ip_address_peer_cmd,
      "ip address A.B.C.D peer A.B.C.D/M",
      "Interface Internet Protocol config commands\n"
      "Set the IP address of an interface\n"
      "Local IP (e.g. 10.0.0.1) for P-t-P address\n"
      "Specify P-t-P address\n"
      "Peer IP address (e.g. 10.0.0.1/8)\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	return ip_address_install(vty, ifp, argv[2]->arg, argv[4]->arg, NULL);
}

DEFUN(no_ip_address_peer,
      no_ip_address_peer_cmd,
      "no ip address A.B.C.D peer A.B.C.D/M",
      NO_STR
      "Interface Internet Protocol config commands\n"
      "Set the IP address of an interface\n"
      "Local IP (e.g. 10.0.0.1) for P-t-P address\n"
      "Specify P-t-P address\n"
      "Peer IP address (e.g. 10.0.0.1/8)\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	return ip_address_uninstall(vty, ifp, argv[3]->arg, argv[5]->arg, NULL);
}

#ifdef HAVE_NETLINK
DEFUN (ip_address_label,
       ip_address_label_cmd,
       "ip address A.B.C.D/M label LINE",
       "Interface Internet Protocol config commands\n"
       "Set the IP address of an interface\n"
       "IP address (e.g. 10.0.0.1/8)\n"
       "Label of this address\n"
       "Label\n")
{
	int idx_ipv4_prefixlen = 2;
	int idx_line = 4;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	return ip_address_install(vty, ifp, argv[idx_ipv4_prefixlen]->arg, NULL,
				  argv[idx_line]->arg);
}

DEFUN (no_ip_address_label,
       no_ip_address_label_cmd,
       "no ip address A.B.C.D/M label LINE",
       NO_STR
       "Interface Internet Protocol config commands\n"
       "Set the IP address of an interface\n"
       "IP address (e.g. 10.0.0.1/8)\n"
       "Label of this address\n"
       "Label\n")
{
	int idx_ipv4_prefixlen = 3;
	int idx_line = 5;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	return ip_address_uninstall(vty, ifp, argv[idx_ipv4_prefixlen]->arg,
				    NULL, argv[idx_line]->arg);
}
#endif /* HAVE_NETLINK */

int if_ipv6_address_install(struct interface *ifp, struct prefix *prefix,
			    const char *label)
{
	struct zebra_if *if_data;
	struct prefix_ipv6 cp;
	struct connected *ifc;
	struct prefix_ipv6 *p;
	enum zebra_dplane_result dplane_res;

	if_data = ifp->info;

	cp.family = prefix->family;
	cp.prefixlen = prefix->prefixlen;
	cp.prefix = prefix->u.prefix6;
	apply_mask_ipv6(&cp);

	ifc = connected_check(ifp, (struct prefix *)&cp);
	if (!ifc) {
		ifc = connected_new();
		ifc->ifp = ifp;

		/* Address. */
		p = prefix_ipv6_new();
		*p = cp;
		ifc->address = (struct prefix *)p;

		/* Label. */
		if (label)
			ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);

		/* Add to linked list. */
		listnode_add(ifp->connected, ifc);
	}

	/* This address is configured from zebra. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
		SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);

	/* In case of this route need to install kernel. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED) &&
	    CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE) &&
	    !(if_data && if_data->shutdown == IF_ZEBRA_DATA_ON)) {
		/* Some system need to up the interface to set IP address. */
		if (!if_is_up(ifp)) {
			if_set_flags(ifp, IFF_UP | IFF_RUNNING);
			if_refresh(ifp);
		}

		dplane_res = dplane_intf_addr_set(ifp, ifc);
		if (dplane_res == ZEBRA_DPLANE_REQUEST_FAILURE) {
			zlog_debug(
				"dplane can't set interface IP address: %s.",
				dplane_res2str(dplane_res));
			return NB_ERR;
		}

		SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
		/* The address will be advertised to zebra clients when the
		 * notification
		 * from the kernel has been received. */
	}

	return 0;
}

static int ipv6_address_install(struct vty *vty, struct interface *ifp,
				const char *addr_str, const char *peer_str,
				const char *label)
{
	struct zebra_if *if_data;
	struct prefix_ipv6 cp;
	struct connected *ifc;
	struct prefix_ipv6 *p;
	int ret;
	enum zebra_dplane_result dplane_res;

	if_data = ifp->info;

	ret = str2prefix_ipv6(addr_str, &cp);
	if (ret <= 0) {
		vty_out(vty, "%% Malformed address \n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (ipv6_martian(&cp.prefix)) {
		vty_out(vty, "%% Invalid address\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	ifc = connected_check(ifp, (struct prefix *)&cp);
	if (!ifc) {
		ifc = connected_new();
		ifc->ifp = ifp;

		/* Address. */
		p = prefix_ipv6_new();
		*p = cp;
		ifc->address = (struct prefix *)p;

		/* Label. */
		if (label)
			ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);

		/* Add to linked list. */
		listnode_add(ifp->connected, ifc);
	}

	/* This address is configured from zebra. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
		SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);

	/* In case of this route need to install kernel. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED) &&
	    CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE) &&
	    !(if_data && if_data->shutdown == IF_ZEBRA_DATA_ON)) {
		/* Some system need to up the interface to set IP address. */
		if (!if_is_up(ifp)) {
			if_set_flags(ifp, IFF_UP | IFF_RUNNING);
			if_refresh(ifp);
		}

		dplane_res = dplane_intf_addr_set(ifp, ifc);
		if (dplane_res == ZEBRA_DPLANE_REQUEST_FAILURE) {
			vty_out(vty, "%% Can't set interface IP address: %s.\n",
				dplane_res2str(dplane_res));
			return CMD_WARNING_CONFIG_FAILED;
		}

		SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
		/* The address will be advertised to zebra clients when the
		 * notification
		 * from the kernel has been received. */
	}

	return CMD_SUCCESS;
}

/* Return true if an ipv6 address is configured on ifp */
int ipv6_address_configured(struct interface *ifp)
{
	struct connected *connected;
	struct listnode *node;

	for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected))
		if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
		    && (connected->address->family == AF_INET6))
			return 1;

	return 0;
}

static int ipv6_address_uninstall(struct vty *vty, struct interface *ifp,
				  const char *addr_str, const char *peer_str,
				  const char *label)
{
	struct prefix_ipv6 cp;
	struct connected *ifc;
	int ret;
	enum zebra_dplane_result dplane_res;

	/* Convert to prefix structure. */
	ret = str2prefix_ipv6(addr_str, &cp);
	if (ret <= 0) {
		vty_out(vty, "%% Malformed address \n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check current interface address. */
	ifc = connected_check(ifp, (struct prefix *)&cp);
	if (!ifc) {
		vty_out(vty, "%% Can't find address\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* This is not configured address. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
		return CMD_WARNING_CONFIG_FAILED;

	UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);

	/* This is not real address or interface is not active. */
	if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
	    || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
		listnode_delete(ifp->connected, ifc);
		connected_free(&ifc);
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* This is real route. */
	dplane_res = dplane_intf_addr_unset(ifp, ifc);
	if (dplane_res == ZEBRA_DPLANE_REQUEST_FAILURE) {
		vty_out(vty, "%% Can't unset interface IP address: %s.\n",
			dplane_res2str(dplane_res));
		return CMD_WARNING_CONFIG_FAILED;
	}

	UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
	/* This information will be propagated to the zclients when the
	 * kernel notification is received. */
	return CMD_SUCCESS;
}

DEFUN (ipv6_address,
       ipv6_address_cmd,
       "ipv6 address X:X::X:X/M",
       "Interface IPv6 config commands\n"
       "Set the IP address of an interface\n"
       "IPv6 address (e.g. 3ffe:506::1/48)\n")
{
	int idx_ipv6_prefixlen = 2;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	return ipv6_address_install(vty, ifp, argv[idx_ipv6_prefixlen]->arg,
				    NULL, NULL);
}

DEFUN (no_ipv6_address,
       no_ipv6_address_cmd,
       "no ipv6 address X:X::X:X/M",
       NO_STR
       "Interface IPv6 config commands\n"
       "Set the IP address of an interface\n"
       "IPv6 address (e.g. 3ffe:506::1/48)\n")
{
	int idx_ipv6_prefixlen = 3;
	VTY_DECLVAR_CONTEXT(interface, ifp);
	return ipv6_address_uninstall(vty, ifp, argv[idx_ipv6_prefixlen]->arg,
				      NULL, NULL);
}

static int link_params_config_write(struct vty *vty, struct interface *ifp)
{
	const struct lyd_node *dnode;
	char xpath[XPATH_MAXLEN];
	int i;

	if ((ifp == NULL) || !HAS_LINK_PARAMS(ifp))
		return -1;

	struct if_link_params *iflp = ifp->link_params;

	vty_out(vty, " link-params\n");
	vty_out(vty, "  enable\n");
	if (IS_PARAM_SET(iflp, LP_TE_METRIC) && iflp->te_metric != ifp->metric)
		vty_out(vty, "  metric %u\n", iflp->te_metric);
	if (IS_PARAM_SET(iflp, LP_MAX_BW) && iflp->max_bw != iflp->default_bw)
		vty_out(vty, "  max-bw %g\n", iflp->max_bw);
	if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW)
	    && iflp->max_rsv_bw != iflp->default_bw)
		vty_out(vty, "  max-rsv-bw %g\n", iflp->max_rsv_bw);
	if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
		for (i = 0; i < 8; i++)
			if (iflp->unrsv_bw[i] != iflp->default_bw)
				vty_out(vty, "  unrsv-bw %d %g\n", i,
					iflp->unrsv_bw[i]);
	}

	snprintf(
		xpath, sizeof(xpath),
		"/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params",
		ifp->name);
	dnode = yang_dnode_get(running_config->dnode, xpath);
	if (dnode)
		nb_cli_show_dnode_cmds(vty, dnode, false);

	if (IS_PARAM_SET(iflp, LP_DELAY)) {
		vty_out(vty, "  delay %u", iflp->av_delay);
		if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
			vty_out(vty, " min %u", iflp->min_delay);
			vty_out(vty, " max %u", iflp->max_delay);
		}
		vty_out(vty, "\n");
	}
	if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
		vty_out(vty, "  delay-variation %u\n", iflp->delay_var);
	if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
		vty_out(vty, "  packet-loss %g\n", iflp->pkt_loss);
	if (IS_PARAM_SET(iflp, LP_AVA_BW))
		vty_out(vty, "  ava-bw %g\n", iflp->ava_bw);
	if (IS_PARAM_SET(iflp, LP_RES_BW))
		vty_out(vty, "  res-bw %g\n", iflp->res_bw);
	if (IS_PARAM_SET(iflp, LP_USE_BW))
		vty_out(vty, "  use-bw %g\n", iflp->use_bw);
	if (IS_PARAM_SET(iflp, LP_RMT_AS))
		vty_out(vty, "  neighbor %pI4 as %u\n", &iflp->rmt_ip,
			iflp->rmt_as);

	vty_out(vty, " exit-link-params\n");
	return 0;
}

static int if_config_write(struct vty *vty)
{
	struct vrf *vrf;
	struct interface *ifp;

	zebra_ptm_write(vty);

	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
		FOR_ALL_INTERFACES (vrf, ifp) {
			struct zebra_if *if_data;
			struct listnode *addrnode;
			struct connected *ifc;
			struct prefix *p;

			if_data = ifp->info;

			if_vty_config_start(vty, ifp);

			if (if_data) {
				if (if_data->shutdown == IF_ZEBRA_DATA_ON)
					vty_out(vty, " shutdown\n");

				zebra_ptm_if_write(vty, if_data);
			}

			if (ifp->desc)
				vty_out(vty, " description %s\n", ifp->desc);

			/* Assign bandwidth here to avoid unnecessary interface
			   flap
			   while processing config script */
			if (ifp->bandwidth != 0)
				vty_out(vty, " bandwidth %u\n", ifp->bandwidth);

			if (!CHECK_FLAG(ifp->status,
					ZEBRA_INTERFACE_LINKDETECTION))
				vty_out(vty, " no link-detect\n");

			for (ALL_LIST_ELEMENTS_RO(ifp->connected, addrnode,
						  ifc)) {
				if (CHECK_FLAG(ifc->conf,
					       ZEBRA_IFC_CONFIGURED)) {
					char buf[INET6_ADDRSTRLEN];
					p = ifc->address;
					vty_out(vty, " ip%s address %s",
						p->family == AF_INET ? ""
								     : "v6",
						inet_ntop(p->family,
							  &p->u.prefix, buf,
							  sizeof(buf)));
					if (CONNECTED_PEER(ifc)) {
						p = ifc->destination;
						vty_out(vty, " peer %s",
							inet_ntop(p->family,
								  &p->u.prefix,
								  buf,
								  sizeof(buf)));
					}
					vty_out(vty, "/%d", p->prefixlen);

					if (ifc->label)
						vty_out(vty, " label %s",
							ifc->label);

					vty_out(vty, "\n");
				}
			}

			if (if_data) {
				if (if_data->multicast != IF_ZEBRA_DATA_UNSPEC)
					vty_out(vty, " %smulticast\n",
						if_data->multicast ==
								IF_ZEBRA_DATA_ON
							? ""
							: "no ");

				if (if_data->mpls_config == IF_ZEBRA_DATA_ON)
					vty_out(vty, " mpls enable\n");
				else if (if_data->mpls_config ==
					 IF_ZEBRA_DATA_OFF)
					vty_out(vty, " mpls disable\n");
			}

			hook_call(zebra_if_config_wr, vty, ifp);
			zebra_evpn_mh_if_write(vty, ifp);
			link_params_config_write(vty, ifp);

			if_vty_config_end(vty);
		}
	return 0;
}

/* Allocate and initialize interface vector. */
void zebra_if_init(void)
{
	/* Initialize interface and new hook. */
	hook_register_prio(if_add, 0, if_zebra_new_hook);
	hook_register_prio(if_del, 0, if_zebra_delete_hook);

	/* Install configuration write function. */
	if_cmd_init(if_config_write);
	install_node(&link_params_node);
	/*
	 * This is *intentionally* setting this to NULL, signaling
	 * that interface creation for zebra acts differently
	 */
	if_zapi_callbacks(NULL, NULL, NULL, NULL);

	install_element(VIEW_NODE, &show_interface_cmd);
	install_element(VIEW_NODE, &show_interface_vrf_all_cmd);
	install_element(VIEW_NODE, &show_interface_name_vrf_cmd);
	install_element(VIEW_NODE, &show_interface_name_vrf_all_cmd);

	install_element(ENABLE_NODE, &show_interface_desc_cmd);
	install_element(ENABLE_NODE, &show_interface_desc_vrf_all_cmd);
	install_element(INTERFACE_NODE, &multicast_cmd);
	install_element(INTERFACE_NODE, &no_multicast_cmd);
	install_element(INTERFACE_NODE, &mpls_cmd);
	install_element(INTERFACE_NODE, &linkdetect_cmd);
	install_element(INTERFACE_NODE, &no_linkdetect_cmd);
	install_element(INTERFACE_NODE, &shutdown_if_cmd);
	install_element(INTERFACE_NODE, &no_shutdown_if_cmd);
	install_element(INTERFACE_NODE, &bandwidth_if_cmd);
	install_element(INTERFACE_NODE, &no_bandwidth_if_cmd);
	install_element(INTERFACE_NODE, &ip_address_cmd);
	install_element(INTERFACE_NODE, &no_ip_address_cmd);
	install_element(INTERFACE_NODE, &ip_address_peer_cmd);
	install_element(INTERFACE_NODE, &no_ip_address_peer_cmd);
	install_element(INTERFACE_NODE, &ipv6_address_cmd);
	install_element(INTERFACE_NODE, &no_ipv6_address_cmd);
#ifdef HAVE_NETLINK
	install_element(INTERFACE_NODE, &ip_address_label_cmd);
	install_element(INTERFACE_NODE, &no_ip_address_label_cmd);
#endif /* HAVE_NETLINK */
	install_element(INTERFACE_NODE, &link_params_cmd);
	install_default(LINK_PARAMS_NODE);
	install_element(LINK_PARAMS_NODE, &link_params_enable_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_enable_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_metric_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_metric_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_maxbw_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_max_rsv_bw_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_unrsv_bw_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_admin_grp_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_admin_grp_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_inter_as_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_inter_as_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_delay_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_delay_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_delay_var_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_delay_var_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_pkt_loss_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_pkt_loss_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_ava_bw_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_ava_bw_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_res_bw_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_res_bw_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_use_bw_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_use_bw_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_affinity_cmd);
	install_element(LINK_PARAMS_NODE, &link_params_affinity_mode_cmd);
	install_element(LINK_PARAMS_NODE, &no_link_params_affinity_mode_cmd);
	install_element(LINK_PARAMS_NODE, &exit_link_params_cmd);

	/* setup EVPN MH elements */
	zebra_evpn_interface_init();
}