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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/* Zebra MPLS code
* Copyright (C) 2013 Cumulus Networks, Inc.
*/
#include <zebra.h>
#include "prefix.h"
#include "table.h"
#include "memory.h"
#include "command.h"
#include "if.h"
#include "log.h"
#include "sockunion.h"
#include "linklist.h"
#include "frrevent.h"
#include "workqueue.h"
#include "prefix.h"
#include "routemap.h"
#include "stream.h"
#include "nexthop.h"
#include "termtable.h"
#include "lib/json.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/interface.h"
#include "zebra/zserv.h"
#include "zebra/zebra_router.h"
#include "zebra/redistribute.h"
#include "zebra/debug.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_mpls.h"
#include "zebra/zebra_srte.h"
#include "zebra/zebra_errors.h"
DEFINE_MTYPE_STATIC(ZEBRA, LSP, "MPLS LSP object");
DEFINE_MTYPE_STATIC(ZEBRA, FEC, "MPLS FEC object");
DEFINE_MTYPE_STATIC(ZEBRA, NHLFE, "MPLS nexthop object");
bool mpls_enabled;
bool mpls_pw_reach_strict; /* Strict reachability checking */
/* static function declarations */
static void fec_evaluate(struct zebra_vrf *zvrf);
static uint32_t fec_derive_label_from_index(struct zebra_vrf *vrf,
struct zebra_fec *fec);
static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
struct route_node *rn, struct route_entry *re);
static int lsp_uninstall(struct zebra_vrf *zvrf, mpls_label_t label);
static int fec_change_update_lsp(struct zebra_vrf *zvrf, struct zebra_fec *fec,
mpls_label_t old_label);
static int fec_send(struct zebra_fec *fec, struct zserv *client);
static void fec_update_clients(struct zebra_fec *fec);
static void fec_print(struct zebra_fec *fec, struct vty *vty);
static struct zebra_fec *fec_find(struct route_table *table, struct prefix *p);
static struct zebra_fec *fec_add(struct route_table *table, struct prefix *p,
mpls_label_t label, uint32_t flags,
uint32_t label_index);
static int fec_del(struct zebra_fec *fec);
static unsigned int label_hash(const void *p);
static bool label_cmp(const void *p1, const void *p2);
static int nhlfe_nexthop_active_ipv4(struct zebra_nhlfe *nhlfe,
struct nexthop *nexthop);
static int nhlfe_nexthop_active_ipv6(struct zebra_nhlfe *nhlfe,
struct nexthop *nexthop);
static int nhlfe_nexthop_active(struct zebra_nhlfe *nhlfe);
static void lsp_select_best_nhlfe(struct zebra_lsp *lsp);
static void lsp_uninstall_from_kernel(struct hash_bucket *bucket, void *ctxt);
static void lsp_schedule(struct hash_bucket *bucket, void *ctxt);
static wq_item_status lsp_process(struct work_queue *wq, void *data);
static void lsp_processq_del(struct work_queue *wq, void *data);
static void lsp_processq_complete(struct work_queue *wq);
static int lsp_processq_add(struct zebra_lsp *lsp);
static void *lsp_alloc(void *p);
/* Check whether lsp can be freed - no nhlfes, e.g., and call free api */
static void lsp_check_free(struct hash *lsp_table, struct zebra_lsp **plsp);
/* Free lsp; sets caller's pointer to NULL */
static void lsp_free(struct hash *lsp_table, struct zebra_lsp **plsp);
static char *nhlfe2str(const struct zebra_nhlfe *nhlfe, char *buf, int size);
static char *nhlfe_config_str(const struct zebra_nhlfe *nhlfe, char *buf,
int size);
static int nhlfe_nhop_match(struct zebra_nhlfe *nhlfe,
enum nexthop_types_t gtype,
const union g_addr *gate, ifindex_t ifindex);
static struct zebra_nhlfe *nhlfe_find(struct nhlfe_list_head *list,
enum lsp_types_t lsp_type,
enum nexthop_types_t gtype,
const union g_addr *gate,
ifindex_t ifindex);
static struct zebra_nhlfe *
nhlfe_add(struct zebra_lsp *lsp, enum lsp_types_t lsp_type,
enum nexthop_types_t gtype, const union g_addr *gate,
ifindex_t ifindex, vrf_id_t vrf_id, uint8_t num_labels,
const mpls_label_t *labels, bool is_backup);
static int nhlfe_del(struct zebra_nhlfe *nhlfe);
static void nhlfe_free(struct zebra_nhlfe *nhlfe);
static void nhlfe_out_label_update(struct zebra_nhlfe *nhlfe,
struct mpls_label_stack *nh_label);
static int mpls_lsp_uninstall_all(struct hash *lsp_table, struct zebra_lsp *lsp,
enum lsp_types_t type);
static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
mpls_label_t in_label);
static void nhlfe_print(struct zebra_nhlfe *nhlfe, struct vty *vty,
const char *indent);
static void lsp_print(struct vty *vty, struct zebra_lsp *lsp);
static void mpls_lsp_uninstall_all_type(struct hash_bucket *bucket, void *ctxt);
static void mpls_ftn_uninstall_all(struct zebra_vrf *zvrf,
int afi, enum lsp_types_t lsp_type);
static int lsp_znh_install(struct zebra_lsp *lsp, enum lsp_types_t type,
const struct zapi_nexthop *znh);
static int lsp_backup_znh_install(struct zebra_lsp *lsp, enum lsp_types_t type,
const struct zapi_nexthop *znh);
/* Static functions */
/*
* Handle failure in LSP install, clear flags for NHLFE.
*/
static void clear_nhlfe_installed(struct zebra_lsp *lsp)
{
struct zebra_nhlfe *nhlfe;
struct nexthop *nexthop;
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
}
frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
}
}
/*
* Install label forwarding entry based on labeled-route entry.
*/
static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
struct route_node *rn, struct route_entry *re)
{
struct hash *lsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
struct nexthop *nexthop;
enum lsp_types_t lsp_type;
char buf[BUFSIZ];
int added, changed;
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return -1;
lsp_type = lsp_type_from_re_type(re->type);
added = changed = 0;
/* Locate or allocate LSP entry. */
tmp_ile.in_label = label;
lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
/* For each active nexthop, create NHLFE. Note that we deliberately skip
* recursive nexthops right now, because intermediate hops won't
* understand
* the label advertised by the recursive nexthop (plus we don't have the
* logic yet to push multiple labels).
*/
for (nexthop = re->nhe->nhg.nexthop;
nexthop; nexthop = nexthop->next) {
/* Skip inactive and recursive entries. */
if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
continue;
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
continue;
nhlfe = nhlfe_find(&lsp->nhlfe_list, lsp_type,
nexthop->type, &nexthop->gate,
nexthop->ifindex);
if (nhlfe) {
/* Clear deleted flag (in case it was set) */
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
if (nexthop_labels_match(nhlfe->nexthop, nexthop))
/* No change */
continue;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, BUFSIZ);
zlog_debug(
"LSP in-label %u type %d nexthop %s out-label changed",
lsp->ile.in_label, lsp_type, buf);
}
/* Update out label, trigger processing. */
nhlfe_out_label_update(nhlfe, nexthop->nh_label);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
changed++;
} else {
/* Add LSP entry to this nexthop */
nhlfe = nhlfe_add(
lsp, lsp_type, nexthop->type, &nexthop->gate,
nexthop->ifindex, nexthop->vrf_id,
nexthop->nh_label->num_labels,
nexthop->nh_label->label, false /*backup*/);
if (!nhlfe)
return -1;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, BUFSIZ);
zlog_debug(
"Add LSP in-label %u type %d nexthop %s out-label %u",
lsp->ile.in_label, lsp_type, buf,
nexthop->nh_label->label[0]);
}
lsp->addr_family = NHLFE_FAMILY(nhlfe);
/* Mark NHLFE as changed. */
SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
added++;
}
}
/* Queue LSP for processing if necessary. If no NHLFE got added (special
* case), delete the LSP entry; this case results in somewhat ugly
* logging.
*/
if (added || changed) {
if (lsp_processq_add(lsp))
return -1;
} else {
lsp_check_free(lsp_table, &lsp);
}
return 0;
}
/*
* Uninstall all non-static NHLFEs of a label forwarding entry. If all
* NHLFEs are removed, the entire entry is deleted.
*/
static int lsp_uninstall(struct zebra_vrf *zvrf, mpls_label_t label)
{
struct hash *lsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
char buf[BUFSIZ];
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return -1;
/* If entry is not present, exit. */
tmp_ile.in_label = label;
lsp = hash_lookup(lsp_table, &tmp_ile);
if (!lsp || (nhlfe_list_first(&lsp->nhlfe_list) == NULL))
return 0;
/* Mark NHLFEs for delete or directly delete, as appropriate. */
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
/* Skip static NHLFEs */
if (nhlfe->type == ZEBRA_LSP_STATIC)
continue;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, BUFSIZ);
zlog_debug(
"Del LSP in-label %u type %d nexthop %s flags 0x%x",
label, nhlfe->type, buf, nhlfe->flags);
}
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)) {
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
} else {
nhlfe_del(nhlfe);
}
}
/* Queue LSP for processing, if needed, else delete. */
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
if (lsp_processq_add(lsp))
return -1;
} else {
lsp_check_free(lsp_table, &lsp);
}
return 0;
}
/*
* This function is invoked upon change to label block configuration; it
* will walk all registered FECs with label-index and appropriately update
* their local labels and trigger client updates.
*/
static void fec_evaluate(struct zebra_vrf *zvrf)
{
struct route_node *rn;
struct zebra_fec *fec;
uint32_t old_label, new_label;
int af;
for (af = AFI_IP; af < AFI_MAX; af++) {
if (zvrf->fec_table[af] == NULL)
continue;
for (rn = route_top(zvrf->fec_table[af]); rn;
rn = route_next(rn)) {
if ((fec = rn->info) == NULL)
continue;
/* Skip configured FECs and those without a label index.
*/
if (fec->flags & FEC_FLAG_CONFIGURED
|| fec->label_index == MPLS_INVALID_LABEL_INDEX)
continue;
/* Save old label, determine new label. */
old_label = fec->label;
new_label =
zvrf->mpls_srgb.start_label + fec->label_index;
if (new_label >= zvrf->mpls_srgb.end_label)
new_label = MPLS_INVALID_LABEL;
/* If label has changed, update FEC and clients. */
if (new_label == old_label)
continue;
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug(
"Update fec %pRN new label %u upon label block",
rn, new_label);
fec->label = new_label;
fec_update_clients(fec);
/* Update label forwarding entries appropriately */
fec_change_update_lsp(zvrf, fec, old_label);
}
}
}
/*
* Derive (if possible) and update the local label for the FEC based on
* its label index. The index is "acceptable" if it falls within the
* globally configured label block (SRGB).
*/
static uint32_t fec_derive_label_from_index(struct zebra_vrf *zvrf,
struct zebra_fec *fec)
{
uint32_t label;
if (fec->label_index != MPLS_INVALID_LABEL_INDEX
&& zvrf->mpls_srgb.start_label
&& ((label = zvrf->mpls_srgb.start_label + fec->label_index)
< zvrf->mpls_srgb.end_label))
fec->label = label;
else
fec->label = MPLS_INVALID_LABEL;
return fec->label;
}
/*
* There is a change for this FEC. Install or uninstall label forwarding
* entries, as appropriate.
*/
static int fec_change_update_lsp(struct zebra_vrf *zvrf, struct zebra_fec *fec,
mpls_label_t old_label)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *re;
afi_t afi;
/* Uninstall label forwarding entry, if previously installed. */
if (old_label != MPLS_INVALID_LABEL
&& old_label != MPLS_LABEL_IMPLICIT_NULL)
lsp_uninstall(zvrf, old_label);
/* Install label forwarding entry corr. to new label, if needed. */
if (fec->label == MPLS_INVALID_LABEL
|| fec->label == MPLS_LABEL_IMPLICIT_NULL)
return 0;
afi = family2afi(PREFIX_FAMILY(&fec->rn->p));
table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
if (!table)
return 0;
/* See if labeled route exists. */
rn = route_node_lookup(table, &fec->rn->p);
if (!rn)
return 0;
RNODE_FOREACH_RE (rn, re) {
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
break;
}
if (!re || !zebra_rib_labeled_unicast(re))
return 0;
if (lsp_install(zvrf, fec->label, rn, re))
return -1;
return 0;
}
/*
* Inform about FEC to a registered client.
*/
static int fec_send(struct zebra_fec *fec, struct zserv *client)
{
struct stream *s;
struct route_node *rn;
rn = fec->rn;
/* Get output stream. */
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_FEC_UPDATE, VRF_DEFAULT);
stream_putw(s, rn->p.family);
stream_put_prefix(s, &rn->p);
stream_putl(s, fec->label);
stream_putw_at(s, 0, stream_get_endp(s));
return zserv_send_message(client, s);
}
/*
* Update all registered clients about this FEC. Caller should've updated
* FEC and ensure no duplicate updates.
*/
static void fec_update_clients(struct zebra_fec *fec)
{
struct listnode *node;
struct zserv *client;
for (ALL_LIST_ELEMENTS_RO(fec->client_list, node, client)) {
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Update client %s",
zebra_route_string(client->proto));
fec_send(fec, client);
}
}
/*
* Print a FEC-label binding entry.
*/
static void fec_print(struct zebra_fec *fec, struct vty *vty)
{
struct route_node *rn;
struct listnode *node;
struct zserv *client;
char buf[BUFSIZ];
rn = fec->rn;
vty_out(vty, "%pRN\n", rn);
vty_out(vty, " Label: %s", label2str(fec->label, 0, buf, BUFSIZ));
if (fec->label_index != MPLS_INVALID_LABEL_INDEX)
vty_out(vty, ", Label Index: %u", fec->label_index);
vty_out(vty, "\n");
if (!list_isempty(fec->client_list)) {
vty_out(vty, " Client list:");
for (ALL_LIST_ELEMENTS_RO(fec->client_list, node, client))
vty_out(vty, " %s(fd %d)",
zebra_route_string(client->proto),
client->sock);
vty_out(vty, "\n");
}
}
/*
* Locate FEC-label binding that matches with passed info.
*/
static struct zebra_fec *fec_find(struct route_table *table, struct prefix *p)
{
struct route_node *rn;
apply_mask(p);
rn = route_node_lookup(table, p);
if (!rn)
return NULL;
route_unlock_node(rn);
return (rn->info);
}
/*
* Add a FEC. This may be upon a client registering for a binding
* or when a binding is configured.
*/
static struct zebra_fec *fec_add(struct route_table *table, struct prefix *p,
mpls_label_t label, uint32_t flags,
uint32_t label_index)
{
struct route_node *rn;
struct zebra_fec *fec;
apply_mask(p);
/* Lookup (or add) route node.*/
rn = route_node_get(table, p);
if (!rn)
return NULL;
fec = rn->info;
if (!fec) {
fec = XCALLOC(MTYPE_FEC, sizeof(struct zebra_fec));
rn->info = fec;
fec->rn = rn;
fec->label = label;
fec->client_list = list_new();
} else
route_unlock_node(rn); /* for the route_node_get */
fec->label_index = label_index;
fec->flags = flags;
return fec;
}
/*
* Delete a FEC. This may be upon the last client deregistering for
* a FEC and no binding exists or when the binding is deleted and there
* are no registered clients.
*/
static int fec_del(struct zebra_fec *fec)
{
list_delete(&fec->client_list);
fec->rn->info = NULL;
route_unlock_node(fec->rn);
XFREE(MTYPE_FEC, fec);
return 0;
}
/*
* Hash function for label.
*/
static unsigned int label_hash(const void *p)
{
const struct zebra_ile *ile = p;
return (jhash_1word(ile->in_label, 0));
}
/*
* Compare 2 LSP hash entries based on in-label.
*/
static bool label_cmp(const void *p1, const void *p2)
{
const struct zebra_ile *ile1 = p1;
const struct zebra_ile *ile2 = p2;
return (ile1->in_label == ile2->in_label);
}
/*
* Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
* the passed flag.
* NOTE: Looking only for connected routes right now.
*/
static int nhlfe_nexthop_active_ipv4(struct zebra_nhlfe *nhlfe,
struct nexthop *nexthop)
{
struct route_table *table;
struct prefix_ipv4 p;
struct route_node *rn;
struct route_entry *match;
struct nexthop *match_nh;
table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, nexthop->vrf_id);
if (!table)
return 0;
/* Lookup nexthop in IPv4 routing table. */
memset(&p, 0, sizeof(p));
p.family = AF_INET;
p.prefixlen = IPV4_MAX_BITLEN;
p.prefix = nexthop->gate.ipv4;
rn = route_node_match(table, (struct prefix *)&p);
if (!rn)
return 0;
route_unlock_node(rn);
/* Locate a valid connected route. */
RNODE_FOREACH_RE (rn, match) {
if (CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED)
|| !CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED))
continue;
for (match_nh = match->nhe->nhg.nexthop; match_nh;
match_nh = match_nh->next) {
if (match->type == ZEBRA_ROUTE_CONNECT
|| nexthop->ifindex == match_nh->ifindex) {
nexthop->ifindex = match_nh->ifindex;
return 1;
}
}
}
return 0;
}
/*
* Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
* the passed flag.
* NOTE: Looking only for connected routes right now.
*/
static int nhlfe_nexthop_active_ipv6(struct zebra_nhlfe *nhlfe,
struct nexthop *nexthop)
{
struct route_table *table;
struct prefix_ipv6 p;
struct route_node *rn;
struct route_entry *match;
table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, nexthop->vrf_id);
if (!table)
return 0;
/* Lookup nexthop in IPv6 routing table. */
memset(&p, 0, sizeof(p));
p.family = AF_INET6;
p.prefixlen = IPV6_MAX_BITLEN;
p.prefix = nexthop->gate.ipv6;
rn = route_node_match(table, (struct prefix *)&p);
if (!rn)
return 0;
route_unlock_node(rn);
/* Locate a valid connected route. */
RNODE_FOREACH_RE (rn, match) {
if ((match->type == ZEBRA_ROUTE_CONNECT)
&& !CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED)
&& CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED))
break;
}
if (!match || !match->nhe->nhg.nexthop)
return 0;
nexthop->ifindex = match->nhe->nhg.nexthop->ifindex;
return 1;
}
/*
* Check the nexthop reachability for a NHLFE and return if valid (reachable)
* or not.
* NOTE: Each NHLFE points to only 1 nexthop.
*/
static int nhlfe_nexthop_active(struct zebra_nhlfe *nhlfe)
{
struct nexthop *nexthop;
struct interface *ifp;
struct zebra_ns *zns;
nexthop = nhlfe->nexthop;
if (!nexthop) // unexpected
return 0;
/* Check on nexthop based on type. */
switch (nexthop->type) {
case NEXTHOP_TYPE_IFINDEX:
/*
* Lookup if this type is special. The
* NEXTHOP_TYPE_IFINDEX is a pop and
* forward into a different table for
* processing. As such this ifindex
* passed to us may be a VRF device
* which will not be in the default
* VRF. So let's look in all of them
*/
zns = zebra_ns_lookup(NS_DEFAULT);
ifp = if_lookup_by_index_per_ns(zns, nexthop->ifindex);
if (ifp && if_is_operative(ifp))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
if (nhlfe_nexthop_active_ipv4(nhlfe, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV6:
if (nhlfe_nexthop_active_ipv6(nhlfe, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
ifp = if_lookup_by_index(nexthop->ifindex,
nexthop->vrf_id);
if (ifp && if_is_operative(ifp))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
} else {
if (nhlfe_nexthop_active_ipv6(nhlfe, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
break;
case NEXTHOP_TYPE_BLACKHOLE:
break;
}
return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
/*
* Walk through NHLFEs for a LSP forwarding entry, verify nexthop
* reachability and select the best. Multipath entries are also
* marked. This is invoked when an LSP scheduled for processing (due
* to some change) is examined.
*/
static void lsp_select_best_nhlfe(struct zebra_lsp *lsp)
{
struct zebra_nhlfe *nhlfe;
struct zebra_nhlfe *best;
struct nexthop *nexthop;
int changed = 0;
if (!lsp)
return;
best = NULL;
lsp->num_ecmp = 0;
UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
/*
* First compute the best path, after checking nexthop status. We are
* only concerned with non-deleted NHLFEs.
*/
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
/* Clear selection flags. */
UNSET_FLAG(nhlfe->flags,
(NHLFE_FLAG_SELECTED | NHLFE_FLAG_MULTIPATH));
if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED)
&& nhlfe_nexthop_active(nhlfe)) {
if (!best || (nhlfe->distance < best->distance))
best = nhlfe;
}
}
lsp->best_nhlfe = best;
if (!lsp->best_nhlfe)
return;
/*
* Check the active status of backup nhlfes also
*/
frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
(void)nhlfe_nexthop_active(nhlfe);
}
/* Mark best NHLFE as selected. */
SET_FLAG(lsp->best_nhlfe->flags, NHLFE_FLAG_SELECTED);
/*
* If best path exists, see if there is ECMP. While doing this, note if
* a
* new (uninstalled) NHLFE has been selected, an installed entry that is
* still selected has a change or an installed entry is to be removed.
*/
frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
int nh_chg, nh_sel, nh_inst;
nexthop = nhlfe->nexthop;
if (!nexthop) // unexpected
continue;
if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED)
&& CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
&& (nhlfe->distance == lsp->best_nhlfe->distance)) {
SET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_MULTIPATH);
lsp->num_ecmp++;
}
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) && !changed) {
nh_chg = CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
nh_sel = CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
nh_inst =
CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
if ((nh_sel && !nh_inst)
|| (nh_sel && nh_inst && nh_chg)
|| (nh_inst && !nh_sel))
changed = 1;
}
/* We have finished examining, clear changed flag. */
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
}
if (changed)
SET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
}
/*
* Delete LSP forwarding entry from kernel, if installed. Called upon
* process exit.
*/
static void lsp_uninstall_from_kernel(struct hash_bucket *bucket, void *ctxt)
{
struct zebra_lsp *lsp;
lsp = (struct zebra_lsp *)bucket->data;
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
(void)dplane_lsp_delete(lsp);
}
/*
* Schedule LSP forwarding entry for processing. Called upon changes
* that may impact LSPs such as nexthop / connected route changes.
*/
static void lsp_schedule(struct hash_bucket *bucket, void *ctxt)
{
struct zebra_lsp *lsp;
lsp = (struct zebra_lsp *)bucket->data;
/* In the common flow, this is used when external events occur. For
* LSPs with backup nhlfes, we'll assume that the forwarding
* plane will use the backups to handle these events, until the
* owning protocol can react.
*/
if (ctxt == NULL) {
/* Skip LSPs with backups */
if (nhlfe_list_first(&lsp->backup_nhlfe_list) != NULL) {
if (IS_ZEBRA_DEBUG_MPLS_DETAIL)
zlog_debug("%s: skip LSP in-label %u",
__func__, lsp->ile.in_label);
return;
}
}
(void)lsp_processq_add(lsp);
}
/*
* Process a LSP entry that is in the queue. Recalculate best NHLFE and
* any multipaths and update or delete from the kernel, as needed.
*/
static wq_item_status lsp_process(struct work_queue *wq, void *data)
{
struct zebra_lsp *lsp;
struct zebra_nhlfe *oldbest, *newbest;
char buf[BUFSIZ], buf2[BUFSIZ];
struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
enum zebra_dplane_result res;
lsp = (struct zebra_lsp *)data;
if (!lsp) // unexpected
return WQ_SUCCESS;
oldbest = lsp->best_nhlfe;
/* Select best NHLFE(s) */
lsp_select_best_nhlfe(lsp);
newbest = lsp->best_nhlfe;
if (IS_ZEBRA_DEBUG_MPLS) {
if (oldbest)
nhlfe2str(oldbest, buf, sizeof(buf));
if (newbest)
nhlfe2str(newbest, buf2, sizeof(buf2));
zlog_debug(
"Process LSP in-label %u oldbest %s newbest %s flags 0x%x ecmp# %d",
lsp->ile.in_label, oldbest ? buf : "NULL",
newbest ? buf2 : "NULL", lsp->flags, lsp->num_ecmp);
}
if (!CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
/* Not already installed */
if (newbest) {
UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
switch (dplane_lsp_add(lsp)) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
/* Set 'installed' flag so we will know
* that an install is in-flight.
*/
SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
zvrf->lsp_installs_queued++;
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
"LSP Install Failure: %u",
lsp->ile.in_label);
break;
case ZEBRA_DPLANE_REQUEST_SUCCESS:
zvrf->lsp_installs++;
break;
}
}
} else {
/* Installed, may need an update and/or delete. */
if (!newbest) {
res = dplane_lsp_delete(lsp);
/* We do some of the lsp cleanup immediately for
* deletes.
*/
UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
clear_nhlfe_installed(lsp);
switch (res) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
zvrf->lsp_removals_queued++;
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE,
"LSP Deletion Failure: %u",
lsp->ile.in_label);
break;
case ZEBRA_DPLANE_REQUEST_SUCCESS:
zvrf->lsp_removals++;
break;
}
} else if (CHECK_FLAG(lsp->flags, LSP_FLAG_CHANGED)) {
struct zebra_nhlfe *nhlfe;
struct nexthop *nexthop;
UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
/* We leave the INSTALLED flag set here
* so we know an update is in-flight.
*/
/*
* Any NHLFE that was installed but is not
* selected now needs to have its flags updated.
*/
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
if (CHECK_FLAG(nhlfe->flags,
NHLFE_FLAG_INSTALLED)
&& !CHECK_FLAG(nhlfe->flags,
NHLFE_FLAG_SELECTED)) {
UNSET_FLAG(nhlfe->flags,
NHLFE_FLAG_INSTALLED);
UNSET_FLAG(nexthop->flags,
NEXTHOP_FLAG_FIB);
}
}
switch (dplane_lsp_update(lsp)) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
zvrf->lsp_installs_queued++;
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
"LSP Update Failure: %u",
lsp->ile.in_label);
break;
case ZEBRA_DPLANE_REQUEST_SUCCESS:
zvrf->lsp_installs++;
break;
}
}
}
return WQ_SUCCESS;
}
/*
* Callback upon processing completion of a LSP forwarding entry.
*/
static void lsp_processq_del(struct work_queue *wq, void *data)
{
struct zebra_vrf *zvrf;
struct zebra_lsp *lsp;
struct hash *lsp_table;
struct zebra_nhlfe *nhlfe;
/* If zebra is shutting down, don't delete any structs,
* just ignore this callback. The LSPs will be cleaned up
* during the shutdown processing.
*/
if (zebra_router_in_shutdown())
return;
zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
lsp_table = zvrf->lsp_table;
if (!lsp_table) // unexpected
return;
lsp = (struct zebra_lsp *)data;
if (!lsp) // unexpected
return;
/* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
* exist,
* delete LSP entry also.
*/
UNSET_FLAG(lsp->flags, LSP_FLAG_SCHEDULED);
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
nhlfe_del(nhlfe);
}
frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
nhlfe_del(nhlfe);
}
lsp_check_free(lsp_table, &lsp);
}
/*
* Callback upon finishing the processing of all scheduled
* LSP forwarding entries.
*/
static void lsp_processq_complete(struct work_queue *wq)
{
/* Nothing to do for now. */
}
/*
* Add LSP forwarding entry to queue for subsequent processing.
*/
static int lsp_processq_add(struct zebra_lsp *lsp)
{
/* If already scheduled, exit. */
if (CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED))
return 0;
if (zrouter.lsp_process_q == NULL) {
flog_err(EC_ZEBRA_WQ_NONEXISTENT,
"%s: work_queue does not exist!", __func__);
return -1;
}
work_queue_add(zrouter.lsp_process_q, lsp);
SET_FLAG(lsp->flags, LSP_FLAG_SCHEDULED);
return 0;
}
/*
* Callback to allocate LSP forwarding table entry.
*/
static void *lsp_alloc(void *p)
{
const struct zebra_ile *ile = p;
struct zebra_lsp *lsp;
lsp = XCALLOC(MTYPE_LSP, sizeof(struct zebra_lsp));
lsp->ile = *ile;
nhlfe_list_init(&lsp->nhlfe_list);
nhlfe_list_init(&lsp->backup_nhlfe_list);
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Alloc LSP in-label %u", lsp->ile.in_label);
return ((void *)lsp);
}
/*
* Check whether lsp can be freed - no nhlfes, e.g., and call free api
*/
static void lsp_check_free(struct hash *lsp_table, struct zebra_lsp **plsp)
{
struct zebra_lsp *lsp;
if (plsp == NULL || *plsp == NULL)
return;
lsp = *plsp;
if ((nhlfe_list_first(&lsp->nhlfe_list) == NULL) &&
(nhlfe_list_first(&lsp->backup_nhlfe_list) == NULL) &&
!CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED))
lsp_free(lsp_table, plsp);
}
static void lsp_free_nhlfe(struct zebra_lsp *lsp)
{
struct zebra_nhlfe *nhlfe;
while ((nhlfe = nhlfe_list_first(&lsp->nhlfe_list))) {
nhlfe_list_del(&lsp->nhlfe_list, nhlfe);
nhlfe_free(nhlfe);
}
while ((nhlfe = nhlfe_list_first(&lsp->backup_nhlfe_list))) {
nhlfe_list_del(&lsp->backup_nhlfe_list, nhlfe);
nhlfe_free(nhlfe);
}
}
/*
* Dtor for an LSP: remove from ile hash, release any internal allocations,
* free LSP object.
*/
static void lsp_free(struct hash *lsp_table, struct zebra_lsp **plsp)
{
struct zebra_lsp *lsp;
if (plsp == NULL || *plsp == NULL)
return;
lsp = *plsp;
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Free LSP in-label %u flags 0x%x",
lsp->ile.in_label, lsp->flags);
lsp_free_nhlfe(lsp);
hash_release(lsp_table, &lsp->ile);
XFREE(MTYPE_LSP, lsp);
*plsp = NULL;
}
/*
* Create printable string for NHLFE entry.
*/
static char *nhlfe2str(const struct zebra_nhlfe *nhlfe, char *buf, int size)
{
const struct nexthop *nexthop;
buf[0] = '\0';
nexthop = nhlfe->nexthop;
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, size);
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, size);
break;
case NEXTHOP_TYPE_IFINDEX:
snprintf(buf, size, "Ifindex: %u", nexthop->ifindex);
case NEXTHOP_TYPE_BLACKHOLE:
break;
}
return buf;
}
/*
* Check if NHLFE matches with search info passed.
*/
static int nhlfe_nhop_match(struct zebra_nhlfe *nhlfe,
enum nexthop_types_t gtype,
const union g_addr *gate, ifindex_t ifindex)
{
struct nexthop *nhop;
int cmp = 1;
nhop = nhlfe->nexthop;
if (!nhop)
return 1;
if (nhop->type != gtype)
return 1;
switch (nhop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
cmp = memcmp(&(nhop->gate.ipv4), &(gate->ipv4),
sizeof(struct in_addr));
if (!cmp && nhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
cmp = !(nhop->ifindex == ifindex);
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
cmp = memcmp(&(nhop->gate.ipv6), &(gate->ipv6),
sizeof(struct in6_addr));
if (!cmp && nhop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
cmp = !(nhop->ifindex == ifindex);
break;
case NEXTHOP_TYPE_IFINDEX:
cmp = !(nhop->ifindex == ifindex);
break;
case NEXTHOP_TYPE_BLACKHOLE:
break;
}
return cmp;
}
/*
* Locate NHLFE that matches with passed info.
* TODO: handle vrf_id if vrf backend is netns based
*/
static struct zebra_nhlfe *nhlfe_find(struct nhlfe_list_head *list,
enum lsp_types_t lsp_type,
enum nexthop_types_t gtype,
const union g_addr *gate,
ifindex_t ifindex)
{
struct zebra_nhlfe *nhlfe;
frr_each_safe(nhlfe_list, list, nhlfe) {
if (nhlfe->type != lsp_type)
continue;
if (!nhlfe_nhop_match(nhlfe, gtype, gate, ifindex))
break;
}
return nhlfe;
}
/*
* Allocate and init new NHLFE.
*/
static struct zebra_nhlfe *
nhlfe_alloc(struct zebra_lsp *lsp, enum lsp_types_t lsp_type,
enum nexthop_types_t gtype, const union g_addr *gate,
ifindex_t ifindex, vrf_id_t vrf_id, uint8_t num_labels,
const mpls_label_t *labels)
{
struct zebra_nhlfe *nhlfe;
struct nexthop *nexthop;
assert(lsp);
nhlfe = XCALLOC(MTYPE_NHLFE, sizeof(struct zebra_nhlfe));
nhlfe->lsp = lsp;
nhlfe->type = lsp_type;
nhlfe->distance = lsp_distance(lsp_type);
nexthop = nexthop_new();
nexthop_add_labels(nexthop, lsp_type, num_labels, labels);
nexthop->vrf_id = vrf_id;
nexthop->type = gtype;
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
nexthop->gate.ipv4 = gate->ipv4;
if (ifindex)
nexthop->ifindex = ifindex;
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
nexthop->gate.ipv6 = gate->ipv6;
if (ifindex)
nexthop->ifindex = ifindex;
break;
case NEXTHOP_TYPE_IFINDEX:
nexthop->ifindex = ifindex;
break;
case NEXTHOP_TYPE_BLACKHOLE:
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("%s: invalid: blackhole nexthop", __func__);
nexthop_free(nexthop);
XFREE(MTYPE_NHLFE, nhlfe);
return NULL;
}
nhlfe->nexthop = nexthop;
return nhlfe;
}
/*
* Add primary or backup NHLFE. Base entry must have been created and
* duplicate check done.
*/
static struct zebra_nhlfe *
nhlfe_add(struct zebra_lsp *lsp, enum lsp_types_t lsp_type,
enum nexthop_types_t gtype, const union g_addr *gate,
ifindex_t ifindex, vrf_id_t vrf_id, uint8_t num_labels,
const mpls_label_t *labels, bool is_backup)
{
struct zebra_nhlfe *nhlfe;
if (!lsp)
return NULL;
/* Allocate new object */
nhlfe = nhlfe_alloc(lsp, lsp_type, gtype, gate, ifindex, vrf_id,
num_labels, labels);
if (!nhlfe)
return NULL;
/* Enqueue to LSP: primaries at head of list, backups at tail */
if (is_backup) {
SET_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP);
nhlfe_list_add_tail(&lsp->backup_nhlfe_list, nhlfe);
} else
nhlfe_list_add_head(&lsp->nhlfe_list, nhlfe);
return nhlfe;
}
/*
* Common delete for NHLFEs.
*/
static void nhlfe_free(struct zebra_nhlfe *nhlfe)
{
if (!nhlfe)
return;
/* Free nexthop. */
if (nhlfe->nexthop)
nexthop_free(nhlfe->nexthop);
nhlfe->nexthop = NULL;
XFREE(MTYPE_NHLFE, nhlfe);
}
/*
* Disconnect NHLFE from LSP, and free. Entry must be present on LSP's list.
*/
static int nhlfe_del(struct zebra_nhlfe *nhlfe)
{
struct zebra_lsp *lsp;
if (!nhlfe)
return -1;
lsp = nhlfe->lsp;
if (!lsp)
return -1;
if (nhlfe == lsp->best_nhlfe)
lsp->best_nhlfe = NULL;
/* Unlink from LSP */
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP))
nhlfe_list_del(&lsp->backup_nhlfe_list, nhlfe);
else
nhlfe_list_del(&lsp->nhlfe_list, nhlfe);
nhlfe->lsp = NULL;
nhlfe_free(nhlfe);
return 0;
}
/*
* Update label for NHLFE entry.
*/
static void nhlfe_out_label_update(struct zebra_nhlfe *nhlfe,
struct mpls_label_stack *nh_label)
{
nhlfe->nexthop->nh_label->label[0] = nh_label->label[0];
}
static int mpls_lsp_uninstall_all(struct hash *lsp_table, struct zebra_lsp *lsp,
enum lsp_types_t type)
{
struct zebra_nhlfe *nhlfe;
int schedule_lsp = 0;
char buf[BUFSIZ];
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
schedule_lsp = 1;
/* Mark NHLFEs for delete or directly delete, as appropriate. */
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
/* Skip non-static NHLFEs */
if (nhlfe->type != type)
continue;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, sizeof(buf));
zlog_debug(
"Del LSP in-label %u type %d nexthop %s flags 0x%x",
lsp->ile.in_label, type, buf, nhlfe->flags);
}
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
schedule_lsp = 1;
} else {
nhlfe_del(nhlfe);
}
}
frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
/* Skip non-static NHLFEs */
if (nhlfe->type != type)
continue;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, sizeof(buf));
zlog_debug(
"Del backup LSP in-label %u type %d nexthop %s flags 0x%x",
lsp->ile.in_label, type, buf, nhlfe->flags);
}
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
schedule_lsp = 1;
} else {
nhlfe_del(nhlfe);
}
}
/* Queue LSP for processing, if needed, else delete. */
if (schedule_lsp) {
if (IS_ZEBRA_DEBUG_MPLS) {
zlog_debug("Schedule LSP in-label %u flags 0x%x",
lsp->ile.in_label, lsp->flags);
}
if (lsp_processq_add(lsp))
return -1;
} else {
lsp_check_free(lsp_table, &lsp);
}
return 0;
}
/*
* Uninstall all static NHLFEs for a particular LSP forwarding entry.
* If no other NHLFEs exist, the entry would be deleted.
*/
static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
mpls_label_t in_label)
{
struct hash *lsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return -1;
/* If entry is not present, exit. */
tmp_ile.in_label = in_label;
lsp = hash_lookup(lsp_table, &tmp_ile);
if (!lsp || (nhlfe_list_first(&lsp->nhlfe_list) == NULL))
return 0;
return mpls_lsp_uninstall_all(lsp_table, lsp, ZEBRA_LSP_STATIC);
}
static json_object *nhlfe_json(struct zebra_nhlfe *nhlfe)
{
json_object *json_nhlfe = NULL;
json_object *json_backups = NULL;
json_object *json_label_stack;
struct nexthop *nexthop = nhlfe->nexthop;
int i;
json_nhlfe = json_object_new_object();
json_object_string_add(json_nhlfe, "type", nhlfe_type2str(nhlfe->type));
if (nexthop->nh_label) {
json_object_int_add(json_nhlfe, "outLabel",
nexthop->nh_label->label[0]);
json_label_stack = json_object_new_array();
json_object_object_add(json_nhlfe, "outLabelStack",
json_label_stack);
for (i = 0; i < nexthop->nh_label->num_labels; i++)
json_object_array_add(
json_label_stack,
json_object_new_int(
nexthop->nh_label->label[i]));
}
json_object_int_add(json_nhlfe, "distance", nhlfe->distance);
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED))
json_object_boolean_true_add(json_nhlfe, "installed");
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
json_object_string_addf(json_nhlfe, "nexthop", "%pI4",
&nexthop->gate.ipv4);
if (nexthop->ifindex)
json_object_string_add(json_nhlfe, "interface",
ifindex2ifname(nexthop->ifindex,
nexthop->vrf_id));
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
json_object_string_addf(json_nhlfe, "nexthop", "%pI6",
&nexthop->gate.ipv6);
if (nexthop->ifindex)
json_object_string_add(json_nhlfe, "interface",
ifindex2ifname(nexthop->ifindex,
nexthop->vrf_id));
break;
case NEXTHOP_TYPE_IFINDEX:
if (nexthop->ifindex)
json_object_string_add(json_nhlfe, "interface",
ifindex2ifname(nexthop->ifindex,
nexthop->vrf_id));
break;
case NEXTHOP_TYPE_BLACKHOLE:
break;
}
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
json_backups = json_object_new_array();
for (i = 0; i < nexthop->backup_num; i++) {
json_object_array_add(
json_backups,
json_object_new_int(nexthop->backup_idx[i]));
}
json_object_object_add(json_nhlfe, "backupIndex",
json_backups);
}
return json_nhlfe;
}
/*
* Print the NHLFE for a LSP forwarding entry.
*/
static void nhlfe_print(struct zebra_nhlfe *nhlfe, struct vty *vty,
const char *indent)
{
struct nexthop *nexthop;
char buf[MPLS_LABEL_STRLEN];
nexthop = nhlfe->nexthop;
if (!nexthop || !nexthop->nh_label) // unexpected
return;
vty_out(vty, " type: %s remote label: %s distance: %d\n",
nhlfe_type2str(nhlfe->type),
mpls_label2str(nexthop->nh_label->num_labels,
nexthop->nh_label->label, buf, sizeof(buf),
nexthop->nh_label_type, 0),
nhlfe->distance);
if (indent)
vty_out(vty, "%s", indent);
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
vty_out(vty, " via %pI4", &nexthop->gate.ipv4);
if (nexthop->ifindex)
vty_out(vty, " dev %s",
ifindex2ifname(nexthop->ifindex,
nexthop->vrf_id));
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
vty_out(vty, " via %s",
inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
sizeof(buf)));
if (nexthop->ifindex)
vty_out(vty, " dev %s",
ifindex2ifname(nexthop->ifindex,
nexthop->vrf_id));
break;
case NEXTHOP_TYPE_IFINDEX:
if (nexthop->ifindex)
vty_out(vty, " dev %s",
ifindex2ifname(nexthop->ifindex,
nexthop->vrf_id));
break;
case NEXTHOP_TYPE_BLACKHOLE:
break;
}
vty_out(vty, "%s",
CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP) ? " (backup)"
: "");
vty_out(vty, "%s",
CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) ? " (installed)"
: "");
vty_out(vty, "\n");
}
/*
* Print an LSP forwarding entry.
*/
static void lsp_print(struct vty *vty, struct zebra_lsp *lsp)
{
struct zebra_nhlfe *nhlfe, *backup;
int i, j;
vty_out(vty, "Local label: %u%s\n", lsp->ile.in_label,
CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) ? " (installed)"
: "");
frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
nhlfe_print(nhlfe, vty, NULL);
if (nhlfe->nexthop == NULL ||
!CHECK_FLAG(nhlfe->nexthop->flags,
NEXTHOP_FLAG_HAS_BACKUP))
continue;
/* Backup nhlfes: find backups in backup list */
for (j = 0; j < nhlfe->nexthop->backup_num; j++) {
i = 0;
backup = NULL;
frr_each(nhlfe_list, &lsp->backup_nhlfe_list, backup) {
if (i == nhlfe->nexthop->backup_idx[j])
break;
i++;
}
if (backup) {
vty_out(vty, " [backup %d]", i);
nhlfe_print(backup, vty, " ");
}
}
}
}
/*
* JSON objects for an LSP forwarding entry.
*/
static json_object *lsp_json(struct zebra_lsp *lsp)
{
struct zebra_nhlfe *nhlfe = NULL;
json_object *json = json_object_new_object();
json_object *json_nhlfe_list = json_object_new_array();
json_object_int_add(json, "inLabel", lsp->ile.in_label);
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
json_object_boolean_true_add(json, "installed");
frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe)
json_object_array_add(json_nhlfe_list, nhlfe_json(nhlfe));
json_object_object_add(json, "nexthops", json_nhlfe_list);
json_nhlfe_list = NULL;
frr_each(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
if (json_nhlfe_list == NULL)
json_nhlfe_list = json_object_new_array();
json_object_array_add(json_nhlfe_list, nhlfe_json(nhlfe));
}
if (json_nhlfe_list)
json_object_object_add(json, "backupNexthops", json_nhlfe_list);
return json;
}
/* Return a sorted linked list of the hash contents */
static struct list *hash_get_sorted_list(struct hash *hash, void *cmp)
{
unsigned int i;
struct hash_bucket *hb;
struct list *sorted_list = list_new();
sorted_list->cmp = (int (*)(void *, void *))cmp;
for (i = 0; i < hash->size; i++)
for (hb = hash->index[i]; hb; hb = hb->next)
listnode_add_sort(sorted_list, hb->data);
return sorted_list;
}
/*
* Compare two LSPs based on their label values.
*/
static int lsp_cmp(const struct zebra_lsp *lsp1, const struct zebra_lsp *lsp2)
{
if (lsp1->ile.in_label < lsp2->ile.in_label)
return -1;
if (lsp1->ile.in_label > lsp2->ile.in_label)
return 1;
return 0;
}
/*
* Initialize work queue for processing changed LSPs.
*/
static void mpls_processq_init(void)
{
zrouter.lsp_process_q = work_queue_new(zrouter.master, "LSP processing");
zrouter.lsp_process_q->spec.workfunc = &lsp_process;
zrouter.lsp_process_q->spec.del_item_data = &lsp_processq_del;
zrouter.lsp_process_q->spec.completion_func = &lsp_processq_complete;
zrouter.lsp_process_q->spec.max_retries = 0;
zrouter.lsp_process_q->spec.hold = 10;
}
/*
* Process LSP update results from zebra dataplane.
*/
void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx *ctx)
{
struct zebra_vrf *zvrf;
mpls_label_t label;
struct zebra_ile tmp_ile;
struct hash *lsp_table;
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
struct nexthop *nexthop;
enum dplane_op_e op;
enum zebra_dplane_result status;
enum zebra_sr_policy_update_label_mode update_mode;
op = dplane_ctx_get_op(ctx);
status = dplane_ctx_get_status(ctx);
if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
ctx, dplane_op2str(op),
dplane_ctx_get_in_label(ctx),
dplane_res2str(status));
label = dplane_ctx_get_in_label(ctx);
switch (op) {
case DPLANE_OP_LSP_INSTALL:
case DPLANE_OP_LSP_UPDATE:
/* Look for zebra LSP object */
zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
lsp_table = zvrf->lsp_table;
tmp_ile.in_label = label;
lsp = hash_lookup(lsp_table, &tmp_ile);
if (lsp == NULL) {
if (IS_ZEBRA_DEBUG_DPLANE)
zlog_debug("LSP ctx %p: in-label %u not found",
ctx, dplane_ctx_get_in_label(ctx));
break;
}
/* TODO -- Confirm that this result is still 'current' */
if (status != ZEBRA_DPLANE_REQUEST_SUCCESS) {
UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
clear_nhlfe_installed(lsp);
flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
"LSP Install Failure: in-label %u",
lsp->ile.in_label);
break;
}
/* Update zebra object */
SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) &&
CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
SET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
}
}
update_mode = (op == DPLANE_OP_LSP_INSTALL)
? ZEBRA_SR_POLICY_LABEL_CREATED
: ZEBRA_SR_POLICY_LABEL_UPDATED;
zebra_sr_policy_label_update(label, update_mode);
break;
case DPLANE_OP_LSP_DELETE:
if (status != ZEBRA_DPLANE_REQUEST_SUCCESS) {
flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE,
"LSP Deletion Failure: in-label %u",
dplane_ctx_get_in_label(ctx));
break;
}
zebra_sr_policy_label_update(label,
ZEBRA_SR_POLICY_LABEL_REMOVED);
break;
case DPLANE_OP_LSP_NOTIFY:
case DPLANE_OP_NONE:
case DPLANE_OP_ROUTE_INSTALL:
case DPLANE_OP_ROUTE_UPDATE:
case DPLANE_OP_ROUTE_DELETE:
case DPLANE_OP_ROUTE_NOTIFY:
case DPLANE_OP_NH_INSTALL:
case DPLANE_OP_NH_UPDATE:
case DPLANE_OP_NH_DELETE:
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_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_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_IP_INSTALL:
case DPLANE_OP_NEIGH_IP_DELETE:
case DPLANE_OP_NEIGH_TABLE_UPDATE:
case DPLANE_OP_GRE_SET:
case DPLANE_OP_INTF_ADDR_ADD:
case DPLANE_OP_INTF_ADDR_DEL:
case DPLANE_OP_INTF_NETCONFIG:
case DPLANE_OP_INTF_INSTALL:
case DPLANE_OP_INTF_UPDATE:
case DPLANE_OP_INTF_DELETE:
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;
} /* Switch */
}
/*
* Process LSP installation info from two sets of nhlfes: a set from
* a dplane notification, and a set from the zebra LSP object. Update
* counters of installed nexthops, and return whether the LSP has changed.
*/
static bool compare_notif_nhlfes(const struct nhlfe_list_head *ctx_head,
struct nhlfe_list_head *nhlfe_head,
int *start_counter, int *end_counter)
{
struct zebra_nhlfe *nhlfe;
const struct zebra_nhlfe *ctx_nhlfe;
struct nexthop *nexthop;
const struct nexthop *ctx_nexthop;
int start_count = 0, end_count = 0;
bool changed_p = false;
bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
frr_each_safe(nhlfe_list, nhlfe_head, nhlfe) {
char buf[NEXTHOP_STRLEN];
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
start_count++;
ctx_nhlfe = NULL;
ctx_nexthop = NULL;
frr_each(nhlfe_list_const, ctx_head, ctx_nhlfe) {
ctx_nexthop = ctx_nhlfe->nexthop;
if (!ctx_nexthop)
continue;
if ((ctx_nexthop->type == nexthop->type) &&
nexthop_same(ctx_nexthop, nexthop)) {
/* Matched */
break;
}
}
if (is_debug)
nexthop2str(nexthop, buf, sizeof(buf));
if (ctx_nhlfe && ctx_nexthop) {
if (is_debug) {
const char *tstr = "";
if (!CHECK_FLAG(ctx_nhlfe->flags,
NHLFE_FLAG_INSTALLED))
tstr = "not ";
zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
buf, tstr);
}
/* Test zebra nhlfe install state */
if (CHECK_FLAG(ctx_nhlfe->flags,
NHLFE_FLAG_INSTALLED)) {
if (!CHECK_FLAG(nhlfe->flags,
NHLFE_FLAG_INSTALLED))
changed_p = true;
/* Update counter */
end_count++;
} else {
if (CHECK_FLAG(nhlfe->flags,
NHLFE_FLAG_INSTALLED))
changed_p = true;
}
} else {
/* Not mentioned in lfib set -> uninstalled */
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) ||
CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE) ||
CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
changed_p = true;
}
if (is_debug)
zlog_debug("LSP dplane notif: no match, nh %s",
buf);
}
}
if (start_counter)
*start_counter += start_count;
if (end_counter)
*end_counter += end_count;
return changed_p;
}
/*
* Update an lsp nhlfe list from a dplane context, typically an async
* notification context. Update the LSP list to match the installed
* status from the context's list.
*/
static int update_nhlfes_from_ctx(struct nhlfe_list_head *nhlfe_head,
const struct nhlfe_list_head *ctx_head)
{
int ret = 0;
struct zebra_nhlfe *nhlfe;
const struct zebra_nhlfe *ctx_nhlfe;
struct nexthop *nexthop;
const struct nexthop *ctx_nexthop;
bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
frr_each_safe(nhlfe_list, nhlfe_head, nhlfe) {
char buf[NEXTHOP_STRLEN];
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
ctx_nhlfe = NULL;
ctx_nexthop = NULL;
frr_each(nhlfe_list_const, ctx_head, ctx_nhlfe) {
ctx_nexthop = ctx_nhlfe->nexthop;
if (!ctx_nexthop)
continue;
if ((ctx_nexthop->type == nexthop->type) &&
nexthop_same(ctx_nexthop, nexthop)) {
/* Matched */
break;
}
}
if (is_debug)
nexthop2str(nexthop, buf, sizeof(buf));
if (ctx_nhlfe && ctx_nexthop) {
/* Bring zebra nhlfe install state into sync */
if (CHECK_FLAG(ctx_nhlfe->flags,
NHLFE_FLAG_INSTALLED)) {
if (is_debug)
zlog_debug("%s: matched lsp nhlfe %s (installed)",
__func__, buf);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
} else {
if (is_debug)
zlog_debug("%s: matched lsp nhlfe %s (not installed)",
__func__, buf);
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
}
if (CHECK_FLAG(ctx_nhlfe->nexthop->flags,
NEXTHOP_FLAG_FIB)) {
SET_FLAG(nhlfe->nexthop->flags,
NEXTHOP_FLAG_ACTIVE);
SET_FLAG(nhlfe->nexthop->flags,
NEXTHOP_FLAG_FIB);
} else {
UNSET_FLAG(nhlfe->nexthop->flags,
NEXTHOP_FLAG_ACTIVE);
UNSET_FLAG(nhlfe->nexthop->flags,
NEXTHOP_FLAG_FIB);
}
} else {
/* Not mentioned in lfib set -> uninstalled */
if (is_debug)
zlog_debug("%s: no match for lsp nhlfe %s",
__func__, buf);
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
}
return ret;
}
/*
* Process async dplane notifications.
*/
void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx *ctx)
{
struct zebra_vrf *zvrf;
struct zebra_ile tmp_ile;
struct hash *lsp_table;
struct zebra_lsp *lsp;
const struct nhlfe_list_head *ctx_list;
int start_count = 0, end_count = 0; /* Installed counts */
bool changed_p = false;
bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
enum zebra_sr_policy_update_label_mode update_mode;
if (is_debug)
zlog_debug("LSP dplane notif, in-label %u",
dplane_ctx_get_in_label(ctx));
/* Look for zebra LSP object */
zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
lsp_table = zvrf->lsp_table;
tmp_ile.in_label = dplane_ctx_get_in_label(ctx);
lsp = hash_lookup(lsp_table, &tmp_ile);
if (lsp == NULL) {
if (is_debug)
zlog_debug("dplane LSP notif: in-label %u not found",
dplane_ctx_get_in_label(ctx));
return;
}
/*
* The dataplane/forwarding plane is notifying zebra about the state
* of the nexthops associated with this LSP. First, we take a
* pre-scan pass to determine whether the LSP has transitioned
* from installed -> uninstalled. In that case, we need to have
* the existing state of the LSP objects available before making
* any changes.
*/
ctx_list = dplane_ctx_get_nhlfe_list(ctx);
changed_p = compare_notif_nhlfes(ctx_list, &lsp->nhlfe_list,
&start_count, &end_count);
if (is_debug)
zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
start_count, end_count,
changed_p ? ", changed" : "");
ctx_list = dplane_ctx_get_backup_nhlfe_list(ctx);
if (compare_notif_nhlfes(ctx_list, &lsp->backup_nhlfe_list,
&start_count, &end_count))
/* Avoid accidentally setting back to 'false' */
changed_p = true;
if (is_debug)
zlog_debug("LSP dplane notif: lfib backups, start_count %d, end_count %d%s",
start_count, end_count,
changed_p ? ", changed" : "");
/*
* Has the LSP become uninstalled? We need the existing state of the
* nexthops/nhlfes at this point so we know what to delete.
*/
if (start_count > 0 && end_count == 0) {
/* Inform other lfibs */
dplane_lsp_notif_update(lsp, DPLANE_OP_LSP_DELETE, ctx);
}
/*
* Now we take a second pass and bring the zebra
* nexthop state into sync with the forwarding-plane state.
*/
ctx_list = dplane_ctx_get_nhlfe_list(ctx);
update_nhlfes_from_ctx(&lsp->nhlfe_list, ctx_list);
ctx_list = dplane_ctx_get_backup_nhlfe_list(ctx);
update_nhlfes_from_ctx(&lsp->backup_nhlfe_list, ctx_list);
if (end_count > 0) {
SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
/* SR-TE update too */
if (start_count == 0)
update_mode = ZEBRA_SR_POLICY_LABEL_CREATED;
else
update_mode = ZEBRA_SR_POLICY_LABEL_UPDATED;
zebra_sr_policy_label_update(lsp->ile.in_label, update_mode);
if (changed_p)
dplane_lsp_notif_update(lsp, DPLANE_OP_LSP_UPDATE, ctx);
} else {
/* SR-TE update too */
zebra_sr_policy_label_update(lsp->ile.in_label,
ZEBRA_SR_POLICY_LABEL_REMOVED);
UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
clear_nhlfe_installed(lsp);
}
}
/*
* Install dynamic LSP entry.
*/
int zebra_mpls_lsp_install(struct zebra_vrf *zvrf, struct route_node *rn,
struct route_entry *re)
{
struct route_table *table;
struct zebra_fec *fec;
table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
if (!table)
return -1;
/* See if there is a configured label binding for this FEC. */
fec = fec_find(table, &rn->p);
if (!fec || fec->label == MPLS_INVALID_LABEL)
return 0;
/* We cannot install a label forwarding entry if local label is the
* implicit-null label.
*/
if (fec->label == MPLS_LABEL_IMPLICIT_NULL)
return 0;
if (lsp_install(zvrf, fec->label, rn, re))
return -1;
return 0;
}
/*
* Uninstall dynamic LSP entry, if any.
*/
int zebra_mpls_lsp_uninstall(struct zebra_vrf *zvrf, struct route_node *rn,
struct route_entry *re)
{
struct route_table *table;
struct zebra_fec *fec;
table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
if (!table)
return -1;
/* See if there is a configured label binding for this FEC. */
fec = fec_find(table, &rn->p);
if (!fec || fec->label == MPLS_INVALID_LABEL)
return 0;
/* Uninstall always removes all dynamic NHLFEs. */
return lsp_uninstall(zvrf, fec->label);
}
/*
* Add an NHLFE to an LSP, return the newly-added object. This path only changes
* the LSP object - nothing is scheduled for processing, for example.
*/
struct zebra_nhlfe *
zebra_mpls_lsp_add_nhlfe(struct zebra_lsp *lsp, enum lsp_types_t lsp_type,
enum nexthop_types_t gtype, const union g_addr *gate,
ifindex_t ifindex, uint8_t num_labels,
const mpls_label_t *out_labels)
{
/* Just a public pass-through to the internal implementation */
return nhlfe_add(lsp, lsp_type, gtype, gate, ifindex, VRF_DEFAULT,
num_labels, out_labels, false /*backup*/);
}
/*
* Add a backup NHLFE to an LSP, return the newly-added object.
* This path only changes the LSP object - nothing is scheduled for
* processing, for example.
*/
struct zebra_nhlfe *zebra_mpls_lsp_add_backup_nhlfe(
struct zebra_lsp *lsp, enum lsp_types_t lsp_type,
enum nexthop_types_t gtype, const union g_addr *gate, ifindex_t ifindex,
uint8_t num_labels, const mpls_label_t *out_labels)
{
/* Just a public pass-through to the internal implementation */
return nhlfe_add(lsp, lsp_type, gtype, gate, ifindex, VRF_DEFAULT,
num_labels, out_labels, true);
}
/*
* Add an NHLFE to an LSP based on a nexthop; return the newly-added object
*/
struct zebra_nhlfe *zebra_mpls_lsp_add_nh(struct zebra_lsp *lsp,
enum lsp_types_t lsp_type,
const struct nexthop *nh)
{
struct zebra_nhlfe *nhlfe;
nhlfe = nhlfe_add(
lsp, lsp_type, nh->type, &nh->gate, nh->ifindex, nh->vrf_id,
nh->nh_label ? nh->nh_label->num_labels : 0,
nh->nh_label ? nh->nh_label->label : NULL, false /*backup*/);
return nhlfe;
}
/*
* Add a backup NHLFE to an LSP based on a nexthop;
* return the newly-added object.
*/
struct zebra_nhlfe *zebra_mpls_lsp_add_backup_nh(struct zebra_lsp *lsp,
enum lsp_types_t lsp_type,
const struct nexthop *nh)
{
struct zebra_nhlfe *nhlfe;
nhlfe = nhlfe_add(lsp, lsp_type, nh->type, &nh->gate, nh->ifindex,
nh->vrf_id,
nh->nh_label ? nh->nh_label->num_labels : 0,
nh->nh_label ? nh->nh_label->label : NULL, true);
return nhlfe;
}
/*
* Free an allocated NHLFE
*/
void zebra_mpls_nhlfe_free(struct zebra_nhlfe *nhlfe)
{
/* Just a pass-through to the internal implementation */
nhlfe_free(nhlfe);
}
/*
* Registration from a client for the label binding for a FEC. If a binding
* already exists, it is informed to the client.
* NOTE: If there is a manually configured label binding, that is used.
* Otherwise, if a label index is specified, it means we have to allocate the
* label from a locally configured label block (SRGB), if one exists and index
* is acceptable. If no label index then just register the specified label.
* NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
* by the calling function. Register requests with both will be rejected.
*/
int zebra_mpls_fec_register(struct zebra_vrf *zvrf, struct prefix *p,
uint32_t label, uint32_t label_index,
struct zserv *client)
{
struct route_table *table;
struct zebra_fec *fec;
bool new_client;
bool label_change = false;
uint32_t old_label;
bool have_label_index = (label_index != MPLS_INVALID_LABEL_INDEX);
bool is_configured_fec = false; /* indicate statically configured FEC */
table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
if (!table)
return -1;
if (label != MPLS_INVALID_LABEL && have_label_index) {
flog_err(
EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT,
"Rejecting FEC register for %pFX with both label %u and Label Index %u specified, client %s",
p, label, label_index,
zebra_route_string(client->proto));
return -1;
}
/* Locate FEC */
fec = fec_find(table, p);
if (!fec) {
fec = fec_add(table, p, label, 0, label_index);
if (!fec) {
flog_err(
EC_ZEBRA_FEC_ADD_FAILED,
"Failed to add FEC %pFX upon register, client %s",
p, zebra_route_string(client->proto));
return -1;
}
old_label = MPLS_INVALID_LABEL;
new_client = true;
} else {
/* Check if the FEC has been statically defined in the config */
is_configured_fec = fec->flags & FEC_FLAG_CONFIGURED;
/* Client may register same FEC with different label index. */
new_client =
(listnode_lookup(fec->client_list, client) == NULL);
if (!new_client && fec->label_index == label_index
&& fec->label == label)
/* Duplicate register */
return 0;
/* Save current label, update the FEC */
old_label = fec->label;
fec->label_index = label_index;
}
if (new_client)
listnode_add(fec->client_list, client);
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("FEC %pFX label%s %u %s by client %s%s", p,
have_label_index ? " index" : "",
have_label_index ? label_index : label,
new_client ? "registered" : "updated",
zebra_route_string(client->proto),
is_configured_fec
? ", but using statically configured label"
: "");
/* If not a statically configured FEC, derive the local label
* from label index or use the provided label
*/
if (!is_configured_fec) {
if (have_label_index)
fec_derive_label_from_index(zvrf, fec);
else
fec->label = label;
/* If no label change, exit. */
if (fec->label == old_label)
return 0;
label_change = true;
}
/* If new client or label change, update client and install or uninstall
* label forwarding entry as needed.
*/
/* Inform client of label, if needed. */
if ((new_client && fec->label != MPLS_INVALID_LABEL) || label_change) {
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Update client label %u", fec->label);
fec_send(fec, client);
}
if (new_client || label_change)
return fec_change_update_lsp(zvrf, fec, old_label);
return 0;
}
/*
* Deregistration from a client for the label binding for a FEC. The FEC
* itself is deleted if no other registered clients exist and there is no
* label bound to the FEC.
*/
int zebra_mpls_fec_unregister(struct zebra_vrf *zvrf, struct prefix *p,
struct zserv *client)
{
struct route_table *table;
struct zebra_fec *fec;
table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
if (!table)
return -1;
fec = fec_find(table, p);
if (!fec) {
flog_err(EC_ZEBRA_FEC_RM_FAILED,
"Failed to find FEC %pFX upon unregister, client %s",
p, zebra_route_string(client->proto));
return -1;
}
listnode_delete(fec->client_list, client);
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("FEC %pFX unregistered by client %s", p,
zebra_route_string(client->proto));
/* If not a configured entry, delete the FEC if no other clients. Before
* deleting, see if any LSP needs to be uninstalled.
*/
if (!(fec->flags & FEC_FLAG_CONFIGURED)
&& list_isempty(fec->client_list)) {
mpls_label_t old_label = fec->label;
fec->label = MPLS_INVALID_LABEL; /* reset */
fec_change_update_lsp(zvrf, fec, old_label);
fec_del(fec);
}
return 0;
}
/*
* Cleanup any FECs registered by this client.
*/
static int zebra_mpls_cleanup_fecs_for_client(struct zserv *client)
{
struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
struct route_node *rn;
struct zebra_fec *fec;
struct listnode *node;
struct zserv *fec_client;
int af;
for (af = AFI_IP; af < AFI_MAX; af++) {
if (zvrf->fec_table[af] == NULL)
continue;
for (rn = route_top(zvrf->fec_table[af]); rn;
rn = route_next(rn)) {
fec = rn->info;
if (!fec || list_isempty(fec->client_list))
continue;
for (ALL_LIST_ELEMENTS_RO(fec->client_list, node,
fec_client)) {
if (fec_client == client) {
listnode_delete(fec->client_list,
fec_client);
if (!(fec->flags & FEC_FLAG_CONFIGURED)
&& list_isempty(fec->client_list))
fec_del(fec);
break;
}
}
}
}
return 0;
}
struct lsp_uninstall_args {
struct hash *lsp_table;
enum lsp_types_t type;
};
/*
* Cleanup MPLS labels registered by this client.
*/
static int zebra_mpls_cleanup_zclient_labels(struct zserv *client)
{
struct vrf *vrf;
struct zebra_vrf *zvrf;
RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
struct lsp_uninstall_args args;
zvrf = vrf->info;
if (!zvrf)
continue;
/* Cleanup LSPs. */
args.lsp_table = zvrf->lsp_table;
args.type = lsp_type_from_re_type(client->proto);
hash_iterate(zvrf->lsp_table, mpls_lsp_uninstall_all_type,
&args);
/* Cleanup FTNs. */
mpls_ftn_uninstall_all(zvrf, AFI_IP,
lsp_type_from_re_type(client->proto));
mpls_ftn_uninstall_all(zvrf, AFI_IP6,
lsp_type_from_re_type(client->proto));
}
return 0;
}
/*
* Return FEC (if any) to which this label is bound.
* Note: Only works for per-prefix binding and when the label is not
* implicit-null.
* TODO: Currently walks entire table, can optimize later with another
* hash..
*/
struct zebra_fec *zebra_mpls_fec_for_label(struct zebra_vrf *zvrf,
mpls_label_t label)
{
struct route_node *rn;
struct zebra_fec *fec;
int af;
for (af = AFI_IP; af < AFI_MAX; af++) {
if (zvrf->fec_table[af] == NULL)
continue;
for (rn = route_top(zvrf->fec_table[af]); rn;
rn = route_next(rn)) {
if (!rn->info)
continue;
fec = rn->info;
if (fec->label == label)
return fec;
}
}
return NULL;
}
/*
* Inform if specified label is currently bound to a FEC or not.
*/
int zebra_mpls_label_already_bound(struct zebra_vrf *zvrf, mpls_label_t label)
{
return (zebra_mpls_fec_for_label(zvrf, label) ? 1 : 0);
}
/*
* Add static FEC to label binding. If there are clients registered for this
* FEC, notify them. If there are labeled routes for this FEC, install the
* label forwarding entry.
*/
int zebra_mpls_static_fec_add(struct zebra_vrf *zvrf, struct prefix *p,
mpls_label_t in_label)
{
struct route_table *table;
struct zebra_fec *fec;
mpls_label_t old_label;
int ret = 0;
table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
if (!table)
return -1;
/* Update existing FEC or create a new one. */
fec = fec_find(table, p);
if (!fec) {
fec = fec_add(table, p, in_label, FEC_FLAG_CONFIGURED,
MPLS_INVALID_LABEL_INDEX);
if (!fec) {
flog_err(EC_ZEBRA_FEC_ADD_FAILED,
"Failed to add FEC %pFX upon config", p);
return -1;
}
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Add fec %pFX label %u", p, in_label);
} else {
fec->flags |= FEC_FLAG_CONFIGURED;
if (fec->label == in_label)
/* Duplicate config */
return 0;
/* Label change, update clients. */
old_label = fec->label;
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Update fec %pFX new label %u", p, in_label);
fec->label = in_label;
fec_update_clients(fec);
/* Update label forwarding entries appropriately */
ret = fec_change_update_lsp(zvrf, fec, old_label);
}
return ret;
}
/*
* Remove static FEC to label binding. If there are no clients registered
* for this FEC, delete the FEC; else notify clients
* Note: Upon delete of static binding, if label index exists for this FEC,
* client may need to be updated with derived label.
*/
int zebra_mpls_static_fec_del(struct zebra_vrf *zvrf, struct prefix *p)
{
struct route_table *table;
struct zebra_fec *fec;
mpls_label_t old_label;
table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
if (!table)
return -1;
fec = fec_find(table, p);
if (!fec) {
flog_err(EC_ZEBRA_FEC_RM_FAILED,
"Failed to find FEC %pFX upon delete", p);
return -1;
}
if (IS_ZEBRA_DEBUG_MPLS) {
zlog_debug("Delete fec %pFX label %u label index %u", p,
fec->label, fec->label_index);
}
old_label = fec->label;
fec->flags &= ~FEC_FLAG_CONFIGURED;
fec->label = MPLS_INVALID_LABEL;
/* If no client exists, just delete the FEC. */
if (list_isempty(fec->client_list)) {
fec_del(fec);
return 0;
}
/* Derive the local label (from label index) or reset it. */
fec_derive_label_from_index(zvrf, fec);
/* If there is a label change, update clients. */
if (fec->label == old_label)
return 0;
fec_update_clients(fec);
/* Update label forwarding entries appropriately */
return fec_change_update_lsp(zvrf, fec, old_label);
}
/*
* Display MPLS FEC to label binding configuration (VTY command handler).
*/
int zebra_mpls_write_fec_config(struct vty *vty, struct zebra_vrf *zvrf)
{
struct route_node *rn;
int af;
struct zebra_fec *fec;
int write = 0;
for (af = AFI_IP; af < AFI_MAX; af++) {
if (zvrf->fec_table[af] == NULL)
continue;
for (rn = route_top(zvrf->fec_table[af]); rn;
rn = route_next(rn)) {
if (!rn->info)
continue;
char lstr[BUFSIZ];
fec = rn->info;
if (!(fec->flags & FEC_FLAG_CONFIGURED))
continue;
write = 1;
vty_out(vty, "mpls label bind %pFX %s\n", &rn->p,
label2str(fec->label, 0, lstr, BUFSIZ));
}
}
return write;
}
/*
* Display MPLS FEC to label binding (VTY command handler).
*/
void zebra_mpls_print_fec_table(struct vty *vty, struct zebra_vrf *zvrf)
{
struct route_node *rn;
int af;
for (af = AFI_IP; af < AFI_MAX; af++) {
if (zvrf->fec_table[af] == NULL)
continue;
for (rn = route_top(zvrf->fec_table[af]); rn;
rn = route_next(rn)) {
if (!rn->info)
continue;
fec_print(rn->info, vty);
}
}
}
/*
* Display MPLS FEC to label binding for a specific FEC (VTY command handler).
*/
void zebra_mpls_print_fec(struct vty *vty, struct zebra_vrf *zvrf,
struct prefix *p)
{
struct route_table *table;
struct route_node *rn;
table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
if (!table)
return;
apply_mask(p);
rn = route_node_lookup(table, p);
if (!rn)
return;
route_unlock_node(rn);
if (!rn->info)
return;
fec_print(rn->info, vty);
}
static void mpls_zebra_nhe_update(struct route_entry *re, afi_t afi,
struct nhg_hash_entry *new_nhe)
{
struct nhg_hash_entry *nhe;
nhe = zebra_nhg_rib_find_nhe(new_nhe, afi);
route_entry_update_nhe(re, nhe);
}
static bool ftn_update_nexthop(bool add_p, struct nexthop *nexthop,
enum lsp_types_t type,
const struct zapi_nexthop *znh)
{
if (add_p && nexthop->nh_label_type == ZEBRA_LSP_NONE)
nexthop_add_labels(nexthop, type, znh->label_num, znh->labels);
else if (!add_p && nexthop->nh_label_type == type)
nexthop_del_labels(nexthop);
else
return false;
return true;
}
void zebra_mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
struct prefix *prefix, uint8_t route_type,
uint8_t route_instance)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *re;
struct nexthop *nexthop;
struct nhg_hash_entry *new_nhe;
afi_t afi = family2afi(prefix->family);
/* Lookup table. */
table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
if (!table)
return;
/* Lookup existing route */
rn = route_node_get(table, prefix);
RNODE_FOREACH_RE (rn, re) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
if (re->type == route_type && re->instance == route_instance)
break;
}
if (re == NULL)
return;
/*
* Nexthops are now shared by multiple routes, so we have to make
* a local copy, modify the copy, then update the route.
*/
new_nhe = zebra_nhe_copy(re->nhe, 0);
for (nexthop = new_nhe->nhg.nexthop; nexthop; nexthop = nexthop->next)
nexthop_del_labels(nexthop);
/* Update backup routes/nexthops also, if present. */
if (zebra_nhg_get_backup_nhg(new_nhe) != NULL) {
for (nexthop = new_nhe->backup_info->nhe->nhg.nexthop; nexthop;
nexthop = nexthop->next)
nexthop_del_labels(nexthop);
}
SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
SET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
/* This will create (or ref) a new nhe, so we will discard the local
* temporary nhe
*/
mpls_zebra_nhe_update(re, afi, new_nhe);
zebra_nhg_free(new_nhe);
rib_queue_add(rn);
}
/*
* Iterate through a list of nexthops, for a match for 'znh'. If found,
* update its labels according to 'add_p', and return 'true' if successful.
*/
static bool ftn_update_znh(bool add_p, enum lsp_types_t type,
struct nexthop *head, const struct zapi_nexthop *znh)
{
bool found = false, success = false;
struct nexthop *nexthop;
for (nexthop = head; nexthop; nexthop = nexthop->next) {
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
if (znh->type != NEXTHOP_TYPE_IPV4
&& znh->type != NEXTHOP_TYPE_IPV4_IFINDEX)
continue;
if (!IPV4_ADDR_SAME(&nexthop->gate.ipv4,
&znh->gate.ipv4))
continue;
if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
&& nexthop->ifindex != znh->ifindex)
continue;
found = true;
if (!ftn_update_nexthop(add_p, nexthop, type, znh))
break;
success = true;
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
if (znh->type != NEXTHOP_TYPE_IPV6
&& znh->type != NEXTHOP_TYPE_IPV6_IFINDEX)
continue;
if (!IPV6_ADDR_SAME(&nexthop->gate.ipv6,
&znh->gate.ipv6))
continue;
if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
&& nexthop->ifindex != znh->ifindex)
continue;
found = true;
if (!ftn_update_nexthop(add_p, nexthop, type, znh))
break;
success = true;
break;
case NEXTHOP_TYPE_IFINDEX:
if (znh->type != NEXTHOP_TYPE_IFINDEX)
continue;
if (nexthop->ifindex != znh->ifindex)
continue;
found = true;
if (!ftn_update_nexthop(add_p, nexthop, type, znh))
break;
success = true;
break;
case NEXTHOP_TYPE_BLACKHOLE:
/* Not valid */
continue;
}
if (found)
break;
}
return success;
}
/*
* Install/uninstall LSP and (optionally) FEC-To-NHLFE (FTN) bindings,
* using zapi message info.
* There are several changes that need to be made, in several zebra
* data structures, so we want to do all the work required at once.
*/
void zebra_mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
const struct zapi_labels *zl)
{
int i, counter, ret = 0;
char buf[NEXTHOP_STRLEN];
const struct zapi_nexthop *znh;
struct route_table *table;
struct route_node *rn = NULL;
struct route_entry *re = NULL;
struct nhg_hash_entry *new_nhe = NULL;
bool found;
afi_t afi = AFI_IP;
const struct prefix *prefix = NULL;
struct hash *lsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp = NULL;
/* Prep LSP for add case */
if (add_p) {
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return;
/* Find or create LSP object */
tmp_ile.in_label = zl->local_label;
lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
}
/* Prep for route/FEC update if requested */
if (CHECK_FLAG(zl->message, ZAPI_LABELS_FTN)) {
prefix = &zl->route.prefix;
afi = family2afi(prefix->family);
/* Lookup table. */
table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
if (table) {
/* Lookup existing route */
rn = route_node_get(table, prefix);
RNODE_FOREACH_RE(rn, re) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
if (re->type == zl->route.type &&
re->instance == zl->route.instance)
break;
}
}
if (re) {
/*
* Copy over current nexthops into a temporary group.
* We can't just change the values here since the nhgs
* are shared and if the labels change, we'll need
* to find or create a new nhg. We need to create
* a whole temporary group, make changes to it,
* then attach that to the route.
*/
new_nhe = zebra_nhe_copy(re->nhe, 0);
} else {
/*
* The old version of the zapi code
* attempted to manage LSPs before trying to
* find a route/FEC, so we'll continue that way.
*/
if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_MPLS)
zlog_debug(
"%s: FTN update requested: no route for prefix %pFX",
__func__, prefix);
}
}
/*
* Use info from the zapi nexthops to add/replace/remove LSP/FECs
*/
counter = 0;
for (i = 0; i < zl->nexthop_num; i++) {
znh = &zl->nexthops[i];
/* Attempt LSP update */
if (add_p)
ret = lsp_znh_install(lsp, zl->type, znh);
else
ret = mpls_lsp_uninstall(zvrf, zl->type,
zl->local_label, znh->type,
&znh->gate, znh->ifindex,
false);
if (ret < 0) {
if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_MPLS) {
zapi_nexthop2str(znh, buf, sizeof(buf));
zlog_debug("%s: Unable to %sinstall LSP: label %u, znh %s",
__func__, (add_p ? "" : "un"),
zl->local_label, buf);
}
continue;
}
/* Attempt route/FEC update if requested */
if (re == NULL)
continue;
/* Search the route's nexthops for a match, and update it. */
found = ftn_update_znh(add_p, zl->type, new_nhe->nhg.nexthop,
znh);
if (found) {
counter++;
} else if (IS_ZEBRA_DEBUG_RECV | IS_ZEBRA_DEBUG_MPLS) {
zapi_nexthop2str(znh, buf, sizeof(buf));
zlog_debug(
"%s: Unable to update FEC: prefix %pFX, label %u, znh %s",
__func__, prefix, zl->local_label, buf);
}
}
/*
* Process backup LSPs/nexthop entries also. We associate backup
* LSP info with backup nexthops.
*/
if (!CHECK_FLAG(zl->message, ZAPI_LABELS_HAS_BACKUPS))
goto znh_done;
for (i = 0; i < zl->backup_nexthop_num; i++) {
znh = &zl->backup_nexthops[i];
if (add_p)
ret = lsp_backup_znh_install(lsp, zl->type, znh);
else
ret = mpls_lsp_uninstall(zvrf, zl->type,
zl->local_label,
znh->type, &znh->gate,
znh->ifindex, true);
if (ret < 0) {
if (IS_ZEBRA_DEBUG_RECV ||
IS_ZEBRA_DEBUG_MPLS) {
zapi_nexthop2str(znh, buf, sizeof(buf));
zlog_debug("%s: Unable to %sinstall backup LSP: label %u, znh %s",
__func__, (add_p ? "" : "un"),
zl->local_label, buf);
}
continue;
}
/* Attempt backup nexthop/FEC update if requested */
if (re == NULL || zebra_nhg_get_backup_nhg(new_nhe) == NULL)
continue;
/* Search the route's backup nexthops for a match
* and update it.
*/
found = ftn_update_znh(add_p, zl->type,
new_nhe->backup_info->nhe->nhg.nexthop,
znh);
if (found) {
counter++;
} else if (IS_ZEBRA_DEBUG_RECV | IS_ZEBRA_DEBUG_MPLS) {
zapi_nexthop2str(znh, buf, sizeof(buf));
zlog_debug(
"%s: Unable to update backup FEC: prefix %pFX, label %u, znh %s",
__func__, prefix, zl->local_label, buf);
}
}
znh_done:
/*
* If we made changes, update the route, and schedule it
* for rib processing
*/
if (re != NULL && counter > 0) {
assert(rn != NULL);
SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
SET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
mpls_zebra_nhe_update(re, afi, new_nhe);
rib_queue_add(rn);
}
if (new_nhe)
zebra_nhg_free(new_nhe);
}
/*
* Install/update a NHLFE for an LSP in the forwarding table. This may be
* a new LSP entry or a new NHLFE for an existing in-label or an update of
* the out-label for an existing NHLFE (update case).
*/
static struct zebra_nhlfe *
lsp_add_nhlfe(struct zebra_lsp *lsp, enum lsp_types_t type,
uint8_t num_out_labels, const mpls_label_t *out_labels,
enum nexthop_types_t gtype, const union g_addr *gate,
ifindex_t ifindex, vrf_id_t vrf_id, bool is_backup)
{
struct zebra_nhlfe *nhlfe;
char buf[MPLS_LABEL_STRLEN];
const char *backup_str;
if (is_backup) {
nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype,
gate, ifindex);
backup_str = "backup ";
} else {
nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate,
ifindex);
backup_str = "";
}
if (nhlfe) {
struct nexthop *nh = nhlfe->nexthop;
assert(nh);
/* Clear deleted flag (in case it was set) */
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
if (!nh->nh_label || num_out_labels == 0)
/* No change */
return nhlfe;
if (nh->nh_label &&
nh->nh_label->num_labels == num_out_labels &&
!memcmp(nh->nh_label->label, out_labels,
sizeof(mpls_label_t) * num_out_labels))
/* No change */
return nhlfe;
if (IS_ZEBRA_DEBUG_MPLS) {
char buf2[MPLS_LABEL_STRLEN];
char buf3[MPLS_LABEL_STRLEN];
nhlfe2str(nhlfe, buf, sizeof(buf));
mpls_label2str(num_out_labels, out_labels, buf2,
sizeof(buf2), 0, 0);
mpls_label2str(nh->nh_label->num_labels,
nh->nh_label->label, buf3, sizeof(buf3),
nh->nh_label_type, 0);
zlog_debug("LSP in-label %u type %d %snexthop %s out-label(s) changed to %s (old %s)",
lsp->ile.in_label, type, backup_str, buf,
buf2, buf3);
}
/* Update out label(s), trigger processing. */
if (nh->nh_label && nh->nh_label->num_labels == num_out_labels)
memcpy(nh->nh_label->label, out_labels,
sizeof(mpls_label_t) * num_out_labels);
else {
nexthop_del_labels(nh);
nexthop_add_labels(nh, type, num_out_labels,
out_labels);
}
} else {
/* Add LSP entry to this nexthop */
nhlfe = nhlfe_add(lsp, type, gtype, gate, ifindex, vrf_id,
num_out_labels, out_labels, is_backup);
if (!nhlfe)
return NULL;
if (IS_ZEBRA_DEBUG_MPLS) {
char buf2[MPLS_LABEL_STRLEN];
nhlfe2str(nhlfe, buf, sizeof(buf));
if (num_out_labels)
mpls_label2str(num_out_labels, out_labels, buf2,
sizeof(buf2), 0, 0);
else
snprintf(buf2, sizeof(buf2), "-");
zlog_debug("Add LSP in-label %u type %d %snexthop %s out-label(s) %s",
lsp->ile.in_label, type, backup_str, buf,
buf2);
}
lsp->addr_family = NHLFE_FAMILY(nhlfe);
}
/* Mark NHLFE, queue LSP for processing. */
SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
return nhlfe;
}
/*
* Install an LSP and forwarding entry; used primarily
* from vrf zapi message processing.
* TODO: handle vrf_id parameter when mpls API extends to interface or SRTE
* changes
*/
int mpls_lsp_install(struct zebra_vrf *zvrf, enum lsp_types_t type,
mpls_label_t in_label, uint8_t num_out_labels,
const mpls_label_t *out_labels, enum nexthop_types_t gtype,
const union g_addr *gate, ifindex_t ifindex)
{
struct hash *lsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return -1;
/* Find or create LSP object */
tmp_ile.in_label = in_label;
lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
nhlfe = lsp_add_nhlfe(lsp, type, num_out_labels, out_labels, gtype,
gate, ifindex, VRF_DEFAULT, false /*backup*/);
if (nhlfe == NULL)
return -1;
/* Queue LSP for processing. */
if (lsp_processq_add(lsp))
return -1;
return 0;
}
/*
* Install or replace NHLFE, using info from zapi nexthop
*/
static int lsp_znh_install(struct zebra_lsp *lsp, enum lsp_types_t type,
const struct zapi_nexthop *znh)
{
struct zebra_nhlfe *nhlfe;
nhlfe = lsp_add_nhlfe(lsp, type, znh->label_num, znh->labels, znh->type,
&znh->gate, znh->ifindex, znh->vrf_id,
false /*backup*/);
if (nhlfe == NULL)
return -1;
/* Update backup info if present */
if (CHECK_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
if (znh->backup_num > NEXTHOP_MAX_BACKUPS) {
nhlfe_del(nhlfe);
return -1;
}
nhlfe->nexthop->backup_num = znh->backup_num;
memcpy(nhlfe->nexthop->backup_idx, znh->backup_idx,
znh->backup_num);
SET_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
} else {
/* Ensure there's no stale backup info */
UNSET_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
nhlfe->nexthop->backup_num = 0;
}
/* Queue LSP for processing. */
if (lsp_processq_add(lsp))
return -1;
return 0;
}
/*
* Install/update backup NHLFE for an LSP, using info from a zapi message.
*/
static int lsp_backup_znh_install(struct zebra_lsp *lsp, enum lsp_types_t type,
const struct zapi_nexthop *znh)
{
struct zebra_nhlfe *nhlfe;
nhlfe = lsp_add_nhlfe(lsp, type, znh->label_num, znh->labels, znh->type,
&znh->gate, znh->ifindex, znh->vrf_id,
true /*backup*/);
if (nhlfe == NULL) {
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("%s: unable to add backup nhlfe, label: %u",
__func__, lsp->ile.in_label);
return -1;
}
/* Queue LSP for processing. */
if (lsp_processq_add(lsp))
return -1;
return 0;
}
struct zebra_lsp *mpls_lsp_find(struct zebra_vrf *zvrf, mpls_label_t in_label)
{
struct hash *lsp_table;
struct zebra_ile tmp_ile;
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return NULL;
/* If entry is not present, exit. */
tmp_ile.in_label = in_label;
return hash_lookup(lsp_table, &tmp_ile);
}
/*
* Uninstall a particular NHLFE in the forwarding table. If this is
* the only NHLFE, the entire LSP forwarding entry has to be deleted.
*/
int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
mpls_label_t in_label, enum nexthop_types_t gtype,
const union g_addr *gate, ifindex_t ifindex,
bool backup_p)
{
struct hash *lsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
char buf[NEXTHOP_STRLEN];
bool schedule_lsp = false;
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return -1;
/* If entry is not present, exit. */
tmp_ile.in_label = in_label;
lsp = hash_lookup(lsp_table, &tmp_ile);
if (!lsp)
return 0;
if (backup_p)
nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype,
gate, ifindex);
else
nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate,
ifindex);
if (!nhlfe)
return 0;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, sizeof(buf));
zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
in_label, type, buf, nhlfe->flags);
}
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) ||
CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED))
schedule_lsp = true;
/* Mark NHLFE for delete or directly delete, as appropriate. */
if (schedule_lsp) {
SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Schedule LSP in-label %u flags 0x%x",
lsp->ile.in_label, lsp->flags);
if (lsp_processq_add(lsp))
return -1;
} else {
nhlfe_del(nhlfe);
/* Free LSP entry if no other NHLFEs and not scheduled. */
lsp_check_free(lsp_table, &lsp);
}
return 0;
}
int mpls_lsp_uninstall_all_vrf(struct zebra_vrf *zvrf, enum lsp_types_t type,
mpls_label_t in_label)
{
struct hash *lsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return -1;
/* If entry is not present, exit. */
tmp_ile.in_label = in_label;
lsp = hash_lookup(lsp_table, &tmp_ile);
if (!lsp)
return 0;
return mpls_lsp_uninstall_all(lsp_table, lsp, type);
}
/*
* Uninstall all NHLFEs for a particular LSP forwarding entry.
* If no other NHLFEs exist, the entry would be deleted.
*/
static void mpls_lsp_uninstall_all_type(struct hash_bucket *bucket, void *ctxt)
{
struct lsp_uninstall_args *args = ctxt;
struct zebra_lsp *lsp;
struct hash *lsp_table;
lsp = (struct zebra_lsp *)bucket->data;
if (nhlfe_list_first(&lsp->nhlfe_list) == NULL)
return;
lsp_table = args->lsp_table;
if (!lsp_table)
return;
mpls_lsp_uninstall_all(lsp_table, lsp, args->type);
}
/*
* Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
* LSP type.
*/
static void mpls_ftn_uninstall_all(struct zebra_vrf *zvrf,
int afi, enum lsp_types_t lsp_type)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *re;
struct nexthop *nexthop;
struct nexthop_group *nhg;
bool update;
/* Process routes of interested address-families. */
table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
if (!table)
return;
for (rn = route_top(table); rn; rn = route_next(rn)) {
update = false;
RNODE_FOREACH_RE (rn, re) {
struct nhg_hash_entry *new_nhe;
new_nhe = zebra_nhe_copy(re->nhe, 0);
nhg = &new_nhe->nhg;
for (nexthop = nhg->nexthop; nexthop;
nexthop = nexthop->next) {
if (nexthop->nh_label_type != lsp_type)
continue;
nexthop_del_labels(nexthop);
SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
SET_FLAG(re->status,
ROUTE_ENTRY_LABELS_CHANGED);
update = true;
}
/* Check for backup info and update that also */
nhg = zebra_nhg_get_backup_nhg(new_nhe);
if (nhg != NULL) {
for (nexthop = nhg->nexthop; nexthop;
nexthop = nexthop->next) {
if (nexthop->nh_label_type != lsp_type)
continue;
nexthop_del_labels(nexthop);
SET_FLAG(re->status,
ROUTE_ENTRY_CHANGED);
SET_FLAG(re->status,
ROUTE_ENTRY_LABELS_CHANGED);
update = true;
}
}
if (CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED))
mpls_zebra_nhe_update(re, afi, new_nhe);
zebra_nhg_free(new_nhe);
}
if (update)
rib_queue_add(rn);
}
}
#if defined(HAVE_CUMULUS)
/*
* Check that the label values used in LSP creation are consistent. The
* main criteria is that if there is ECMP, the label operation must still
* be consistent - i.e., all paths either do a swap or do PHP. This is due
* to current HW restrictions.
*/
int zebra_mpls_lsp_label_consistent(struct zebra_vrf *zvrf,
mpls_label_t in_label,
mpls_label_t out_label,
enum nexthop_types_t gtype,
union g_addr *gate, ifindex_t ifindex)
{
struct hash *slsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
const struct nexthop *nh;
/* Lookup table. */
slsp_table = zvrf->slsp_table;
if (!slsp_table)
return 0;
/* If entry is not present, exit. */
tmp_ile.in_label = in_label;
lsp = hash_lookup(slsp_table, &tmp_ile);
if (!lsp)
return 1;
nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC,
gtype, gate, ifindex);
if (nhlfe) {
nh = nhlfe->nexthop;
if (nh == NULL || nh->nh_label == NULL)
return 0;
if (nh->nh_label->label[0] == out_label)
return 1;
/* If not only NHLFE, cannot allow label change. */
if (nhlfe != nhlfe_list_first(&lsp->nhlfe_list) ||
nhlfe_list_next(&lsp->nhlfe_list, nhlfe) != NULL)
return 0;
} else {
/* If other NHLFEs exist, label operation must match. */
nhlfe = nhlfe_list_first(&lsp->nhlfe_list);
if (nhlfe != NULL) {
int cur_op, new_op;
nh = nhlfe->nexthop;
if (nh == NULL || nh->nh_label == NULL)
return 0;
cur_op = (nh->nh_label->label[0] ==
MPLS_LABEL_IMPLICIT_NULL);
new_op = (out_label == MPLS_LABEL_IMPLICIT_NULL);
if (cur_op != new_op)
return 0;
}
}
/* Label values are good. */
return 1;
}
#endif /* HAVE_CUMULUS */
/*
* Add static LSP entry. This may be the first entry for this incoming label
* or an additional nexthop; an existing entry may also have outgoing label
* changed.
* Note: The label operation (swap or PHP) is common for the LSP entry (all
* NHLFEs).
*/
int zebra_mpls_static_lsp_add(struct zebra_vrf *zvrf, mpls_label_t in_label,
mpls_label_t out_label,
enum nexthop_types_t gtype, union g_addr *gate,
ifindex_t ifindex)
{
struct hash *slsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
char buf[BUFSIZ];
/* Lookup table. */
slsp_table = zvrf->slsp_table;
if (!slsp_table)
return -1;
/* Find or create LSP. */
tmp_ile.in_label = in_label;
lsp = hash_get(slsp_table, &tmp_ile, lsp_alloc);
nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC, gtype, gate,
ifindex);
if (nhlfe) {
struct nexthop *nh = nhlfe->nexthop;
assert(nh);
assert(nh->nh_label);
/* Compare existing nexthop */
if (nh->nh_label->num_labels == 1 &&
nh->nh_label->label[0] == out_label)
/* No change */
return 0;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, sizeof(buf));
zlog_debug(
"Upd static LSP in-label %u nexthop %s out-label %u (old %u)",
in_label, buf, out_label,
nh->nh_label->label[0]);
}
if (nh->nh_label->num_labels == 1)
nh->nh_label->label[0] = out_label;
else {
nexthop_del_labels(nh);
nexthop_add_labels(nh, ZEBRA_LSP_STATIC, 1, &out_label);
}
} else {
/* Add static LSP entry to this nexthop */
nhlfe = nhlfe_add(lsp, ZEBRA_LSP_STATIC, gtype, gate, ifindex,
VRF_DEFAULT, 1, &out_label, false /*backup*/);
if (!nhlfe)
return -1;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, sizeof(buf));
zlog_debug(
"Add static LSP in-label %u nexthop %s out-label %u",
in_label, buf, out_label);
}
}
/* (Re)Install LSP in the main table. */
if (mpls_lsp_install(zvrf, ZEBRA_LSP_STATIC, in_label, 1, &out_label,
gtype, gate, ifindex))
return -1;
return 0;
}
/*
* Delete static LSP entry. This may be the delete of one particular
* NHLFE for this incoming label or the delete of the entire entry (i.e.,
* all NHLFEs).
* NOTE: Delete of the only NHLFE will also end up deleting the entire
* LSP configuration.
*/
int zebra_mpls_static_lsp_del(struct zebra_vrf *zvrf, mpls_label_t in_label,
enum nexthop_types_t gtype, union g_addr *gate,
ifindex_t ifindex)
{
struct hash *slsp_table;
struct zebra_ile tmp_ile;
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
/* Lookup table. */
slsp_table = zvrf->slsp_table;
if (!slsp_table)
return -1;
/* If entry is not present, exit. */
tmp_ile.in_label = in_label;
lsp = hash_lookup(slsp_table, &tmp_ile);
if (!lsp)
return 0;
/* Is it delete of entire LSP or a specific NHLFE? */
if (gtype == NEXTHOP_TYPE_BLACKHOLE) {
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Del static LSP in-label %u", in_label);
/* Uninstall entire LSP from the main table. */
mpls_static_lsp_uninstall_all(zvrf, in_label);
/* Delete all static NHLFEs */
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
nhlfe_del(nhlfe);
}
} else {
/* Find specific NHLFE, exit if not found. */
nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC,
gtype, gate, ifindex);
if (!nhlfe)
return 0;
if (IS_ZEBRA_DEBUG_MPLS) {
char buf[BUFSIZ];
nhlfe2str(nhlfe, buf, sizeof(buf));
zlog_debug("Del static LSP in-label %u nexthop %s",
in_label, buf);
}
/* Uninstall LSP from the main table. */
mpls_lsp_uninstall(zvrf, ZEBRA_LSP_STATIC, in_label, gtype,
gate, ifindex, false);
/* Delete static LSP NHLFE */
nhlfe_del(nhlfe);
}
/* Remove entire static LSP entry if no NHLFE - valid in either case
* above.
*/
if (nhlfe_list_first(&lsp->nhlfe_list) == NULL) {
lsp = hash_release(slsp_table, &tmp_ile);
lsp_free_nhlfe(lsp);
XFREE(MTYPE_LSP, lsp);
}
return 0;
}
/*
* Schedule all MPLS label forwarding entries for processing.
* Called upon changes that may affect one or more of them such as
* interface or nexthop state changes.
*/
void zebra_mpls_lsp_schedule(struct zebra_vrf *zvrf)
{
if (!zvrf)
return;
hash_iterate(zvrf->lsp_table, lsp_schedule, NULL);
}
/*
* Display MPLS label forwarding table for a specific LSP
* (VTY command handler).
*/
void zebra_mpls_print_lsp(struct vty *vty, struct zebra_vrf *zvrf,
mpls_label_t label, bool use_json)
{
struct hash *lsp_table;
struct zebra_lsp *lsp;
struct zebra_ile tmp_ile;
json_object *json = NULL;
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table) {
if (use_json)
vty_out(vty, "{}\n");
return;
}
/* If entry is not present, exit. */
tmp_ile.in_label = label;
lsp = hash_lookup(lsp_table, &tmp_ile);
if (!lsp) {
if (use_json)
vty_out(vty, "{}\n");
return;
}
if (use_json) {
json = lsp_json(lsp);
vty_json(vty, json);
} else
lsp_print(vty, lsp);
}
/*
* Display MPLS label forwarding table (VTY command handler).
*/
void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
bool use_json)
{
char buf[BUFSIZ];
json_object *json = NULL;
struct zebra_lsp *lsp = NULL;
struct zebra_nhlfe *nhlfe = NULL;
struct listnode *node = NULL;
struct list *lsp_list = hash_get_sorted_list(zvrf->lsp_table, lsp_cmp);
if (use_json) {
json = json_object_new_object();
for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp))
json_object_object_add(json,
label2str(lsp->ile.in_label, 0,
buf, sizeof(buf)),
lsp_json(lsp));
vty_json(vty, json);
} else {
struct ttable *tt;
/* Prepare table. */
tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
ttable_add_row(tt, "Inbound Label|Type|Nexthop|Outbound Label");
tt->style.cell.rpad = 2;
tt->style.corner = '+';
ttable_restyle(tt);
ttable_rowseps(tt, 0, BOTTOM, true, '-');
for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
struct nexthop *nexthop;
const char *out_label_str;
char nh_buf[NEXTHOP_STRLEN];
nexthop = nhlfe->nexthop;
switch (nexthop->type) {
case NEXTHOP_TYPE_IFINDEX: {
struct zebra_ns *zns;
struct interface *ifp;
zns = zebra_ns_lookup(NS_DEFAULT);
ifp = if_lookup_by_index_per_ns(
zns, nexthop->ifindex);
snprintf(nh_buf, sizeof(nh_buf), "%s",
ifp ? ifp->name : "Null");
break;
}
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
inet_ntop(AF_INET, &nexthop->gate.ipv4,
nh_buf, sizeof(nh_buf));
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
inet_ntop(AF_INET6, &nexthop->gate.ipv6,
nh_buf, sizeof(nh_buf));
break;
case NEXTHOP_TYPE_BLACKHOLE:
break;
}
if (nexthop->type != NEXTHOP_TYPE_IFINDEX &&
nexthop->nh_label)
out_label_str = mpls_label2str(
nexthop->nh_label->num_labels,
&nexthop->nh_label->label[0],
buf, sizeof(buf),
nexthop->nh_label_type, 1);
else
out_label_str = "-";
ttable_add_row(tt, "%u|%s|%s|%s",
lsp->ile.in_label,
nhlfe_type2str(nhlfe->type),
nh_buf, out_label_str);
}
}
/* Dump the generated table. */
if (tt->nrows > 1) {
char *table = ttable_dump(tt, "\n");
vty_out(vty, "%s\n", table);
XFREE(MTYPE_TMP, table);
}
ttable_del(tt);
}
list_delete(&lsp_list);
}
/*
* Create printable string for static LSP configuration.
*/
static char *nhlfe_config_str(const struct zebra_nhlfe *nhlfe, char *buf,
int size)
{
const struct nexthop *nh;
nh = nhlfe->nexthop;
buf[0] = '\0';
switch (nh->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
inet_ntop(AF_INET, &nh->gate.ipv4, buf, size);
if (nh->ifindex)
strlcat(buf, ifindex2ifname(nh->ifindex, VRF_DEFAULT),
size);
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
inet_ntop(AF_INET6, &nh->gate.ipv6, buf, size);
if (nh->ifindex)
strlcat(buf,
ifindex2ifname(nh->ifindex, VRF_DEFAULT),
size);
break;
case NEXTHOP_TYPE_IFINDEX:
if (nh->ifindex)
strlcat(buf,
ifindex2ifname(nh->ifindex, VRF_DEFAULT),
size);
break;
case NEXTHOP_TYPE_BLACKHOLE:
break;
}
return buf;
}
/*
* Display MPLS LSP configuration of all static LSPs (VTY command handler).
*/
int zebra_mpls_write_lsp_config(struct vty *vty, struct zebra_vrf *zvrf)
{
struct zebra_lsp *lsp;
struct zebra_nhlfe *nhlfe;
struct nexthop *nh;
struct listnode *node;
struct list *slsp_list =
hash_get_sorted_list(zvrf->slsp_table, lsp_cmp);
for (ALL_LIST_ELEMENTS_RO(slsp_list, node, lsp)) {
frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
char buf[BUFSIZ];
char lstr[30];
nh = nhlfe->nexthop;
if (nh == NULL || nh->nh_label == NULL)
continue;
nhlfe_config_str(nhlfe, buf, sizeof(buf));
switch (nh->nh_label->label[0]) {
case MPLS_LABEL_IPV4_EXPLICIT_NULL:
case MPLS_LABEL_IPV6_EXPLICIT_NULL:
strlcpy(lstr, "explicit-null", sizeof(lstr));
break;
case MPLS_LABEL_IMPLICIT_NULL:
strlcpy(lstr, "implicit-null", sizeof(lstr));
break;
default:
snprintf(lstr, sizeof(lstr), "%u",
nh->nh_label->label[0]);
break;
}
vty_out(vty, "mpls lsp %u %s %s\n", lsp->ile.in_label,
buf, lstr);
}
}
list_delete(&slsp_list);
return (zvrf->slsp_table->count ? 1 : 0);
}
/*
* Add/update global label block.
*/
int zebra_mpls_label_block_add(struct zebra_vrf *zvrf, uint32_t start_label,
uint32_t end_label)
{
zvrf->mpls_srgb.start_label = start_label;
zvrf->mpls_srgb.end_label = end_label;
/* Evaluate registered FECs to see if any get a label or not. */
fec_evaluate(zvrf);
return 0;
}
/*
* Delete global label block.
*/
int zebra_mpls_label_block_del(struct zebra_vrf *zvrf)
{
zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
/* Process registered FECs to clear their local label, if needed. */
fec_evaluate(zvrf);
return 0;
}
/*
* Display MPLS global label block configuration (VTY command handler).
*/
int zebra_mpls_write_label_block_config(struct vty *vty, struct zebra_vrf *zvrf)
{
if (zvrf->mpls_srgb.start_label == 0)
return 0;
if ((zvrf->mpls_srgb.start_label != MPLS_DEFAULT_MIN_SRGB_LABEL)
|| (zvrf->mpls_srgb.end_label != MPLS_DEFAULT_MAX_SRGB_LABEL)) {
vty_out(vty, "mpls label global-block %u %u\n",
zvrf->mpls_srgb.start_label, zvrf->mpls_srgb.end_label);
}
return 1;
}
/*
* Called when VRF becomes inactive, cleans up information but keeps
* the table itself.
*/
void zebra_mpls_cleanup_tables(struct zebra_vrf *zvrf)
{
struct zebra_vrf *def_zvrf;
afi_t afi;
if (zvrf_id(zvrf) == VRF_DEFAULT)
hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
else {
/*
* For other vrfs, we try to remove associated LSPs; we locate
* the LSPs in the default vrf.
*/
def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
/* At shutdown, the default may be gone already */
if (def_zvrf == NULL)
return;
for (afi = AFI_IP; afi < AFI_MAX; afi++) {
if (zvrf->label[afi] != MPLS_LABEL_NONE)
lsp_uninstall(def_zvrf, zvrf->label[afi]);
}
}
}
/*
* When a vrf label is assigned and the client goes away
* we should cleanup the vrf labels associated with
* that zclient.
*/
void zebra_mpls_client_cleanup_vrf_label(uint8_t proto)
{
struct vrf *vrf;
struct zebra_vrf *def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
if (def_zvrf == NULL)
return;
RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
struct zebra_vrf *zvrf = vrf->info;
afi_t afi;
if (!zvrf)
continue;
for (afi = AFI_IP; afi < AFI_MAX; afi++) {
if (zvrf->label_proto[afi] == proto
&& zvrf->label[afi] != MPLS_LABEL_NONE)
lsp_uninstall(def_zvrf, zvrf->label[afi]);
/*
* Cleanup data structures by fiat
*/
zvrf->label_proto[afi] = 0;
zvrf->label[afi] = MPLS_LABEL_NONE;
}
}
}
static void lsp_table_free(void *p)
{
struct zebra_lsp *lsp = p;
lsp_free_nhlfe(lsp);
XFREE(MTYPE_LSP, lsp);
}
/*
* Called upon process exiting, need to delete LSP forwarding
* entries from the kernel.
* NOTE: Currently supported only for default VRF.
*/
void zebra_mpls_close_tables(struct zebra_vrf *zvrf)
{
hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
hash_clean_and_free(&zvrf->lsp_table, lsp_table_free);
hash_clean_and_free(&zvrf->slsp_table, lsp_table_free);
route_table_finish(zvrf->fec_table[AFI_IP]);
route_table_finish(zvrf->fec_table[AFI_IP6]);
}
/*
* Allocate MPLS tables for this VRF and do other initialization.
* NOTE: Currently supported only for default VRF.
*/
void zebra_mpls_init_tables(struct zebra_vrf *zvrf)
{
char buffer[80];
if (!zvrf)
return;
snprintf(buffer, sizeof(buffer), "ZEBRA SLSP table: %s",
zvrf->vrf->name);
zvrf->slsp_table = hash_create_size(8, label_hash, label_cmp, buffer);
snprintf(buffer, sizeof(buffer), "ZEBRA LSP table: %s",
zvrf->vrf->name);
zvrf->lsp_table = hash_create_size(8, label_hash, label_cmp, buffer);
zvrf->fec_table[AFI_IP] = route_table_init();
zvrf->fec_table[AFI_IP6] = route_table_init();
zvrf->mpls_flags = 0;
zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
}
void zebra_mpls_turned_on(void)
{
if (!mpls_enabled) {
mpls_processq_init();
mpls_enabled = true;
}
hook_register(zserv_client_close, zebra_mpls_cleanup_fecs_for_client);
hook_register(zserv_client_close, zebra_mpls_cleanup_zclient_labels);
}
/*
* Global MPLS initialization.
*/
void zebra_mpls_init(void)
{
mpls_enabled = false;
mpls_pw_reach_strict = false;
if (mpls_kernel_init() < 0) {
flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED,
"Disabling MPLS support (no kernel support)");
return;
}
zebra_mpls_turned_on();
}
|