1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
|
/* Routing Information Base.
* Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "command.h"
#include "if.h"
#include "linklist.h"
#include "log.h"
#include "memory.h"
#include "mpls.h"
#include "nexthop.h"
#include "prefix.h"
#include "prefix.h"
#include "routemap.h"
#include "sockunion.h"
#include "srcdest_table.h"
#include "table.h"
#include "thread.h"
#include "vrf.h"
#include "workqueue.h"
#include "nexthop_group_private.h"
#include "frr_pthread.h"
#include "printfrr.h"
#include "frrscript.h"
#include "zebra/zebra_router.h"
#include "zebra/connected.h"
#include "zebra/debug.h"
#include "zebra/interface.h"
#include "zebra/redistribute.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/zapi_msg.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_rnh.h"
#include "zebra/zebra_routemap.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/zapi_msg.h"
#include "zebra/zebra_dplane.h"
#include "zebra/zebra_evpn_mh.h"
#include "zebra/zebra_script.h"
DEFINE_MGROUP(ZEBRA, "zebra");
DEFINE_MTYPE(ZEBRA, RE, "Route Entry");
DEFINE_MTYPE_STATIC(ZEBRA, RIB_DEST, "RIB destination");
DEFINE_MTYPE_STATIC(ZEBRA, RIB_UPDATE_CTX, "Rib update context object");
DEFINE_MTYPE_STATIC(ZEBRA, WQ_WRAPPER, "WQ wrapper");
/*
* Event, list, and mutex for delivery of dataplane results
*/
static pthread_mutex_t dplane_mutex;
static struct thread *t_dplane;
static struct dplane_ctx_q rib_dplane_q;
DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
(rn, reason));
DEFINE_HOOK(rib_shutdown, (struct route_node * rn), (rn));
/* Meta Q's specific names */
enum meta_queue_indexes {
META_QUEUE_NHG,
META_QUEUE_EVPN,
META_QUEUE_EARLY_ROUTE,
META_QUEUE_EARLY_LABEL,
META_QUEUE_CONNECTED,
META_QUEUE_KERNEL,
META_QUEUE_STATIC,
META_QUEUE_NOTBGP,
META_QUEUE_BGP,
META_QUEUE_OTHER,
};
/* Each route type's string and default distance value. */
static const struct {
int key;
uint8_t distance;
enum meta_queue_indexes meta_q_map;
} route_info[ZEBRA_ROUTE_MAX] = {
[ZEBRA_ROUTE_NHG] = {ZEBRA_ROUTE_NHG, 255 /* Unneeded for nhg's */,
META_QUEUE_NHG},
[ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0, META_QUEUE_KERNEL},
[ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0, META_QUEUE_KERNEL},
[ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0, META_QUEUE_CONNECTED},
[ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1, META_QUEUE_STATIC},
[ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120, META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120, META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110, META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110, META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115, META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */,
META_QUEUE_BGP},
[ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, 255, META_QUEUE_OTHER},
[ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, 90, META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, 10, META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, 255, META_QUEUE_OTHER},
[ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, 255, META_QUEUE_OTHER},
[ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, 150, META_QUEUE_STATIC},
[ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, 150, META_QUEUE_OTHER},
[ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, 20, META_QUEUE_BGP},
[ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT, 20, META_QUEUE_BGP},
[ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH, 20,
META_QUEUE_BGP},
[ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT, 20, META_QUEUE_BGP},
[ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT, 20,
META_QUEUE_BGP},
[ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 100, META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, 150, META_QUEUE_OTHER},
[ZEBRA_ROUTE_PBR] = {ZEBRA_ROUTE_PBR, 200, META_QUEUE_OTHER},
[ZEBRA_ROUTE_BFD] = {ZEBRA_ROUTE_BFD, 255, META_QUEUE_OTHER},
[ZEBRA_ROUTE_OPENFABRIC] = {ZEBRA_ROUTE_OPENFABRIC, 115,
META_QUEUE_NOTBGP},
[ZEBRA_ROUTE_VRRP] = {ZEBRA_ROUTE_VRRP, 255, META_QUEUE_OTHER},
[ZEBRA_ROUTE_SRTE] = {ZEBRA_ROUTE_SRTE, 255, META_QUEUE_OTHER},
[ZEBRA_ROUTE_ALL] = {ZEBRA_ROUTE_ALL, 255, META_QUEUE_OTHER},
/* Any new route type added to zebra, should be mirrored here */
/* no entry/default: 150 */
};
/* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
* from the OS, and an 'nhe' is a nhe update.
*/
struct wq_nhg_wrapper {
int type;
union {
struct nhg_ctx *ctx;
struct nhg_hash_entry *nhe;
} u;
};
#define WQ_NHG_WRAPPER_TYPE_CTX 0x01
#define WQ_NHG_WRAPPER_TYPE_NHG 0x02
/* Wrapper structs for evpn/vxlan workqueue items. */
struct wq_evpn_wrapper {
int type;
bool add_p;
vrf_id_t vrf_id;
bool esr_rxed;
uint8_t df_alg;
uint16_t df_pref;
uint32_t flags;
uint32_t seq;
esi_t esi;
vni_t vni;
struct ipaddr ip;
struct ethaddr macaddr;
struct prefix prefix;
struct in_addr vtep_ip;
};
#define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01
#define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02
#define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03
#define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04
enum wq_label_types {
WQ_LABEL_FTN_UNINSTALL,
WQ_LABEL_LABELS_PROCESS,
};
struct wq_label_wrapper {
enum wq_label_types type;
vrf_id_t vrf_id;
struct prefix p;
enum lsp_types_t ltype;
uint8_t route_type;
uint8_t route_instance;
bool add_p;
struct zapi_labels zl;
int afi;
};
static void rib_addnode(struct route_node *rn, struct route_entry *re,
int process);
/* %pRN is already a printer for route_nodes that just prints the prefix */
#ifdef _FRR_ATTRIBUTE_PRINTFRR
#pragma FRR printfrr_ext "%pZN" (struct route_node *)
#endif
static const char *subqueue2str(enum meta_queue_indexes index)
{
switch (index) {
case META_QUEUE_NHG:
return "NHG Objects";
case META_QUEUE_EVPN:
return "EVPN/VxLan Objects";
case META_QUEUE_EARLY_ROUTE:
return "Early Route Processing";
case META_QUEUE_EARLY_LABEL:
return "Early Label Handling";
case META_QUEUE_CONNECTED:
return "Connected Routes";
case META_QUEUE_KERNEL:
return "Kernel Routes";
case META_QUEUE_STATIC:
return "Static Routes";
case META_QUEUE_NOTBGP:
return "RIP/OSPF/ISIS/EIGRP/NHRP Routes";
case META_QUEUE_BGP:
return "BGP Routes";
case META_QUEUE_OTHER:
return "Other Routes";
}
return "Unknown";
}
printfrr_ext_autoreg_p("ZN", printfrr_zebra_node);
static ssize_t printfrr_zebra_node(struct fbuf *buf, struct printfrr_eargs *ea,
const void *ptr)
{
struct route_node *rn = (struct route_node *)ptr;
ssize_t rv = 0;
/* just the table number? */
if (ea->fmt[0] == 't') {
rib_dest_t *dest;
struct route_entry *re = NULL;
ea->fmt++;
if (!rn)
return bputch(buf, '!');
dest = rib_dest_from_rnode(rn);
if (dest)
re = re_list_first(&dest->routes);
if (re)
rv += bprintfrr(buf, "%u", re->table);
else
rv += bputch(buf, '?');
} else {
char cbuf[PREFIX_STRLEN * 2 + 6];
struct rib_table_info *info;
if (!rn)
return bputs(buf, "{(route_node *) NULL}");
srcdest_rnode2str(rn, cbuf, sizeof(cbuf));
rv += bputs(buf, cbuf);
info = srcdest_rnode_table_info(rn);
if (info->safi == SAFI_MULTICAST)
rv += bputs(buf, " (MRIB)");
}
return rv;
}
#define rnode_debug(node, vrf_id, msg, ...) \
zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
##__VA_ARGS__)
#define rnode_info(node, vrf_id, msg, ...) \
zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
##__VA_ARGS__)
static char *_dump_re_status(const struct route_entry *re, char *buf,
size_t len)
{
if (re->status == 0) {
snprintfrr(buf, len, "None ");
return buf;
}
snprintfrr(
buf, len, "%s%s%s%s%s%s%s",
CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED) ? "Removed " : "",
CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED) ? "Changed " : "",
CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED)
? "Label Changed "
: "",
CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED) ? "Queued " : "",
CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) ? "Installed "
: "",
CHECK_FLAG(re->status, ROUTE_ENTRY_FAILED) ? "Failed " : "",
CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG) ? "Fib NHG "
: "");
return buf;
}
uint8_t route_distance(int type)
{
uint8_t distance;
if ((unsigned)type >= array_size(route_info))
distance = 150;
else
distance = route_info[type].distance;
return distance;
}
int is_zebra_valid_kernel_table(uint32_t table_id)
{
#ifdef linux
if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
|| (table_id == RT_TABLE_COMPAT))
return 0;
#endif
return 1;
}
int is_zebra_main_routing_table(uint32_t table_id)
{
if (table_id == RT_TABLE_MAIN)
return 1;
return 0;
}
int zebra_check_addr(const struct prefix *p)
{
if (p->family == AF_INET) {
uint32_t addr;
addr = p->u.prefix4.s_addr;
addr = ntohl(addr);
if (IPV4_NET127(addr) || IN_CLASSD(addr)
|| IPV4_LINKLOCAL(addr))
return 0;
}
if (p->family == AF_INET6) {
if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6))
return 0;
if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
return 0;
}
return 1;
}
static void route_entry_attach_ref(struct route_entry *re,
struct nhg_hash_entry *new)
{
re->nhe = new;
re->nhe_id = new->id;
re->nhe_installed_id = 0;
zebra_nhg_increment_ref(new);
}
/* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
int route_entry_update_nhe(struct route_entry *re,
struct nhg_hash_entry *new_nhghe)
{
int ret = 0;
struct nhg_hash_entry *old_nhg = NULL;
if (new_nhghe == NULL) {
old_nhg = re->nhe;
re->nhe_id = 0;
re->nhe_installed_id = 0;
re->nhe = NULL;
goto done;
}
if ((re->nhe_id != 0) && re->nhe && (re->nhe != new_nhghe)) {
/* Capture previous nhg, if any */
old_nhg = re->nhe;
route_entry_attach_ref(re, new_nhghe);
} else if (!re->nhe)
/* This is the first time it's being attached */
route_entry_attach_ref(re, new_nhghe);
done:
/* Detach / deref previous nhg */
if (old_nhg)
zebra_nhg_decrement_ref(old_nhg);
return ret;
}
void rib_handle_nhg_replace(struct nhg_hash_entry *old_entry,
struct nhg_hash_entry *new_entry)
{
struct zebra_router_table *zrt;
struct route_node *rn;
struct route_entry *re, *next;
if (IS_ZEBRA_DEBUG_RIB_DETAILED || IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
__func__, new_entry->id, new_entry, old_entry);
/* We have to do them ALL */
RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
for (rn = route_top(zrt->table); rn;
rn = srcdest_route_next(rn)) {
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (re->nhe && re->nhe == old_entry)
route_entry_update_nhe(re, new_entry);
}
}
}
}
struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
const union g_addr *addr,
struct route_node **rn_out)
{
struct prefix p;
struct route_table *table;
struct route_node *rn;
struct route_entry *match = NULL;
/* Lookup table. */
table = zebra_vrf_table(afi, safi, vrf_id);
if (!table)
return 0;
memset(&p, 0, sizeof(p));
p.family = afi;
if (afi == AFI_IP) {
p.u.prefix4 = addr->ipv4;
p.prefixlen = IPV4_MAX_BITLEN;
} else {
p.u.prefix6 = addr->ipv6;
p.prefixlen = IPV6_MAX_BITLEN;
}
rn = route_node_match(table, &p);
while (rn) {
rib_dest_t *dest;
route_unlock_node(rn);
dest = rib_dest_from_rnode(rn);
if (dest && dest->selected_fib
&& !CHECK_FLAG(dest->selected_fib->status,
ROUTE_ENTRY_REMOVED))
match = dest->selected_fib;
/* If there is no selected route or matched route is EGP, go up
tree. */
if (!match) {
do {
rn = rn->parent;
} while (rn && rn->info == NULL);
if (rn)
route_lock_node(rn);
} else {
if (match->type != ZEBRA_ROUTE_CONNECT) {
if (!CHECK_FLAG(match->status,
ROUTE_ENTRY_INSTALLED))
return NULL;
}
if (rn_out)
*rn_out = rn;
return match;
}
}
return NULL;
}
struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id,
struct in_addr addr,
struct route_node **rn_out)
{
struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
struct route_node *m_rn = NULL, *u_rn = NULL;
union g_addr gaddr = {.ipv4 = addr};
switch (zrouter.ipv4_multicast_mode) {
case MCAST_MRIB_ONLY:
return rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
rn_out);
case MCAST_URIB_ONLY:
return rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, rn_out);
case MCAST_NO_CONFIG:
case MCAST_MIX_MRIB_FIRST:
re = mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
&m_rn);
if (!mre)
re = ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id,
&gaddr, &u_rn);
break;
case MCAST_MIX_DISTANCE:
mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
if (mre && ure)
re = ure->distance < mre->distance ? ure : mre;
else if (mre)
re = mre;
else if (ure)
re = ure;
break;
case MCAST_MIX_PFXLEN:
mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
if (mre && ure)
re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
else if (mre)
re = mre;
else if (ure)
re = ure;
break;
}
if (rn_out)
*rn_out = (re == mre) ? m_rn : u_rn;
if (IS_ZEBRA_DEBUG_RIB) {
char buf[BUFSIZ];
inet_ntop(AF_INET, &addr, buf, BUFSIZ);
zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__,
buf, vrf_id_to_name(vrf_id), vrf_id,
mre ? (ure ? "MRIB+URIB" : "MRIB")
: ure ? "URIB" : "nothing",
re == ure ? "URIB" : re == mre ? "MRIB" : "none");
}
return re;
}
struct route_entry *rib_match_ipv6_multicast(vrf_id_t vrf_id,
struct in6_addr addr,
struct route_node **rn_out)
{
struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
struct route_node *m_rn = NULL, *u_rn = NULL;
union g_addr gaddr = {.ipv6 = addr};
switch (zrouter.ipv4_multicast_mode) {
case MCAST_MRIB_ONLY:
return rib_match(AFI_IP6, SAFI_MULTICAST, vrf_id, &gaddr,
rn_out);
case MCAST_URIB_ONLY:
return rib_match(AFI_IP6, SAFI_UNICAST, vrf_id, &gaddr, rn_out);
case MCAST_NO_CONFIG:
case MCAST_MIX_MRIB_FIRST:
re = mre = rib_match(AFI_IP6, SAFI_MULTICAST, vrf_id, &gaddr,
&m_rn);
if (!mre)
re = ure = rib_match(AFI_IP6, SAFI_UNICAST, vrf_id,
&gaddr, &u_rn);
break;
case MCAST_MIX_DISTANCE:
mre = rib_match(AFI_IP6, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
ure = rib_match(AFI_IP6, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
if (mre && ure)
re = ure->distance < mre->distance ? ure : mre;
else if (mre)
re = mre;
else if (ure)
re = ure;
break;
case MCAST_MIX_PFXLEN:
mre = rib_match(AFI_IP6, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
ure = rib_match(AFI_IP6, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
if (mre && ure)
re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
else if (mre)
re = mre;
else if (ure)
re = ure;
break;
}
if (rn_out)
*rn_out = (re == mre) ? m_rn : u_rn;
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%s: %pI6: vrf: %s(%u) found %s, using %s", __func__,
&addr, vrf_id_to_name(vrf_id), vrf_id,
mre ? (ure ? "MRIB+URIB" : "MRIB")
: ure ? "URIB" : "nothing",
re == ure ? "URIB" : re == mre ? "MRIB" : "none");
return re;
}
struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *match = NULL;
rib_dest_t *dest;
/* Lookup table. */
table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
if (!table)
return 0;
rn = route_node_lookup(table, (struct prefix *)p);
/* No route for this prefix. */
if (!rn)
return NULL;
/* Unlock node. */
route_unlock_node(rn);
dest = rib_dest_from_rnode(rn);
if (dest && dest->selected_fib
&& !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
match = dest->selected_fib;
if (!match)
return NULL;
if (match->type == ZEBRA_ROUTE_CONNECT)
return match;
if (CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED))
return match;
return NULL;
}
/*
* Is this RIB labeled-unicast? It must be of type BGP and all paths
* (nexthops) must have a label.
*/
int zebra_rib_labeled_unicast(struct route_entry *re)
{
struct nexthop *nexthop = NULL;
if (re->type != ZEBRA_ROUTE_BGP)
return 0;
for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
return 0;
return 1;
}
/* Update flag indicates whether this is a "replace" or not. Currently, this
* is only used for IPv4.
*/
void rib_install_kernel(struct route_node *rn, struct route_entry *re,
struct route_entry *old)
{
struct nexthop *nexthop;
struct rib_table_info *info = srcdest_rnode_table_info(rn);
struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
const struct prefix *p, *src_p;
enum zebra_dplane_result ret;
rib_dest_t *dest = rib_dest_from_rnode(rn);
srcdest_rnode_prefixes(rn, &p, &src_p);
if (info->safi != SAFI_UNICAST) {
for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
return;
}
/*
* Install the resolved nexthop object first.
*/
zebra_nhg_install_kernel(re->nhe);
/*
* If this is a replace to a new RE let the originator of the RE
* know that they've lost
*/
if (old && (old != re) && (old->type != re->type))
zsend_route_notify_owner(rn, old, ZAPI_ROUTE_BETTER_ADMIN_WON,
info->afi, info->safi);
/* Update fib selection */
dest->selected_fib = re;
/*
* Make sure we update the FPM any time we send new information to
* the kernel.
*/
hook_call(rib_update, rn, "installing in kernel");
/* Send add or update */
if (old)
ret = dplane_route_update(rn, re, old);
else
ret = dplane_route_add(rn, re);
switch (ret) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
SET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
if (old) {
SET_FLAG(old->status, ROUTE_ENTRY_QUEUED);
/* Free old FIB nexthop group */
UNSET_FLAG(old->status, ROUTE_ENTRY_USE_FIB_NHG);
if (old->fib_ng.nexthop) {
nexthops_free(old->fib_ng.nexthop);
old->fib_ng.nexthop = NULL;
}
}
if (zvrf)
zvrf->installs_queued++;
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
{
flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
"%u:%u:%pRN: Failed to enqueue dataplane install",
re->vrf_id, re->table, rn);
break;
}
case ZEBRA_DPLANE_REQUEST_SUCCESS:
if (zvrf)
zvrf->installs++;
break;
}
return;
}
/* Uninstall the route from kernel. */
void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
{
struct nexthop *nexthop;
struct rib_table_info *info = srcdest_rnode_table_info(rn);
struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
if (info->safi != SAFI_UNICAST) {
UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
return;
}
/*
* Make sure we update the FPM any time we send new information to
* the dataplane.
*/
hook_call(rib_update, rn, "uninstalling from kernel");
switch (dplane_route_delete(rn, re)) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
if (zvrf)
zvrf->removals_queued++;
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
"%u:%pRN: Failed to enqueue dataplane uninstall",
re->vrf_id, rn);
break;
case ZEBRA_DPLANE_REQUEST_SUCCESS:
if (zvrf)
zvrf->removals++;
break;
}
return;
}
/*
* rib_can_delete_dest
*
* Returns true if the given dest can be deleted from the table.
*/
static int rib_can_delete_dest(rib_dest_t *dest)
{
if (re_list_first(&dest->routes)) {
return 0;
}
/*
* Unresolved rnh's are stored on the default route's list
*
* dest->rnode can also be the source prefix node in an
* ipv6 sourcedest table. Fortunately the prefix of a
* source prefix node can never be the default prefix.
*/
if (is_default_prefix(&dest->rnode->p))
return 0;
/*
* Don't delete the dest if we have to update the FPM about this
* prefix.
*/
if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
|| CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
return 0;
return 1;
}
void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq,
bool rt_delete)
{
rib_dest_t *dest = rib_dest_from_rnode(rn);
struct rnh *rnh;
/*
* We are storing the rnh's associated withb
* the tracked nexthop as a list of the rn's.
* Unresolved rnh's are placed at the top
* of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
* As such for each rn we need to walk up the tree
* and see if any rnh's need to see if they
* would match a more specific route
*/
while (rn) {
if (IS_ZEBRA_DEBUG_NHT_DETAILED)
zlog_debug(
"%s: %pRN Being examined for Nexthop Tracking Count: %zd",
__func__, rn,
dest ? rnh_list_count(&dest->nht) : 0);
if (rt_delete && (!dest || !rnh_list_count(&dest->nht))) {
if (IS_ZEBRA_DEBUG_NHT_DETAILED)
zlog_debug("%pRN has no tracking NHTs. Bailing",
rn);
break;
}
if (!dest) {
rn = rn->parent;
if (rn)
dest = rib_dest_from_rnode(rn);
continue;
}
/*
* If we have any rnh's stored in the nht list
* then we know that this route node was used for
* nht resolution and as such we need to call the
* nexthop tracking evaluation code
*/
frr_each_safe(rnh_list, &dest->nht, rnh) {
struct zebra_vrf *zvrf =
zebra_vrf_lookup_by_id(rnh->vrf_id);
struct prefix *p = &rnh->node->p;
if (IS_ZEBRA_DEBUG_NHT_DETAILED)
zlog_debug(
"%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
zvrf_name(zvrf), zvrf_id(zvrf), rn,
rnh->node, seq, rnh->seqno);
/*
* If we have evaluated this node on this pass
* already, due to following the tree up
* then we know that we can move onto the next
* rnh to process.
*
* Additionally we call zebra_evaluate_rnh
* when we gc the dest. In this case we know
* that there must be no other re's where
* we were originally as such we know that
* that sequence number is ok to respect.
*/
if (rnh->seqno == seq) {
if (IS_ZEBRA_DEBUG_NHT_DETAILED)
zlog_debug(
" Node processed and moved already");
continue;
}
rnh->seqno = seq;
zebra_evaluate_rnh(zvrf, family2afi(p->family), 0, p,
rnh->safi);
}
rn = rn->parent;
if (rn)
dest = rib_dest_from_rnode(rn);
}
}
/*
* rib_gc_dest
*
* Garbage collect the rib dest corresponding to the given route node
* if appropriate.
*
* Returns true if the dest was deleted, false otherwise.
*/
int rib_gc_dest(struct route_node *rn)
{
rib_dest_t *dest;
dest = rib_dest_from_rnode(rn);
if (!dest)
return 0;
if (!rib_can_delete_dest(dest))
return 0;
if (IS_ZEBRA_DEBUG_RIB) {
struct zebra_vrf *zvrf;
zvrf = rib_dest_vrf(dest);
rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
}
zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
true);
dest->rnode = NULL;
rnh_list_fini(&dest->nht);
XFREE(MTYPE_RIB_DEST, dest);
rn->info = NULL;
/*
* Release the one reference that we keep on the route node.
*/
route_unlock_node(rn);
return 1;
}
void zebra_rtable_node_cleanup(struct route_table *table,
struct route_node *node)
{
struct route_entry *re, *next;
RNODE_FOREACH_RE_SAFE (node, re, next) {
rib_unlink(node, re);
}
if (node->info) {
rib_dest_t *dest = node->info;
/* Remove from update queue of FPM module */
hook_call(rib_shutdown, node);
rnh_list_fini(&dest->nht);
XFREE(MTYPE_RIB_DEST, node->info);
}
}
static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
struct route_entry *new)
{
hook_call(rib_update, rn, "new route selected");
/* Update real nexthop. This may actually determine if nexthop is active
* or not. */
if (!nexthop_group_active_nexthop_num(&(new->nhe->nhg))) {
UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
return;
}
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
zvrf_name(zvrf), zvrf_id(zvrf), new->table, rn, rn,
new, zebra_route_string(new->type));
/* If labeled-unicast route, install transit LSP. */
if (zebra_rib_labeled_unicast(new))
zebra_mpls_lsp_install(zvrf, rn, new);
rib_install_kernel(rn, new, NULL);
UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
}
static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
struct route_entry *old)
{
hook_call(rib_update, rn, "removing existing route");
/* Uninstall from kernel. */
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
zvrf_name(zvrf), zvrf_id(zvrf), old->table, rn, rn,
old, zebra_route_string(old->type));
/* If labeled-unicast route, uninstall transit LSP. */
if (zebra_rib_labeled_unicast(old))
zebra_mpls_lsp_uninstall(zvrf, rn, old);
rib_uninstall_kernel(rn, old);
/* Update nexthop for route, reset changed flag. */
/* Note: this code also handles the Linux case when an interface goes
* down, causing the kernel to delete routes without sending DELROUTE
* notifications
*/
if (RIB_KERNEL_ROUTE(old))
SET_FLAG(old->status, ROUTE_ENTRY_REMOVED);
else
UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
}
static void rib_process_update_fib(struct zebra_vrf *zvrf,
struct route_node *rn,
struct route_entry *old,
struct route_entry *new)
{
int nh_active = 0;
/*
* We have to install or update if a new route has been selected or
* something has changed.
*/
if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
hook_call(rib_update, rn, "updating existing route");
/* Update the nexthop; we could determine here that nexthop is
* inactive. */
if (nexthop_group_active_nexthop_num(&(new->nhe->nhg)))
nh_active = 1;
/* If nexthop is active, install the selected route, if
* appropriate. If
* the install succeeds, cleanup flags for prior route, if
* different from
* newly selected.
*/
if (nh_active) {
if (IS_ZEBRA_DEBUG_RIB) {
if (new != old)
zlog_debug(
"%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
zvrf_name(zvrf), zvrf_id(zvrf),
new->table, rn, rn, new,
zebra_route_string(new->type),
old,
zebra_route_string(old->type));
else
zlog_debug(
"%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
zvrf_name(zvrf), zvrf_id(zvrf),
new->table, rn, rn, new,
zebra_route_string(new->type));
}
/* If labeled-unicast route, uninstall transit LSP. */
if (zebra_rib_labeled_unicast(old))
zebra_mpls_lsp_uninstall(zvrf, rn, old);
/*
* Non-system route should be installed.
* If labeled-unicast route, install transit
* LSP.
*/
if (zebra_rib_labeled_unicast(new))
zebra_mpls_lsp_install(zvrf, rn, new);
rib_install_kernel(rn, new, old);
}
/*
* If nexthop for selected route is not active or install
* failed, we
* may need to uninstall and delete for redistribution.
*/
if (!nh_active) {
if (IS_ZEBRA_DEBUG_RIB) {
if (new != old)
zlog_debug(
"%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
zvrf_name(zvrf), zvrf_id(zvrf),
new->table, rn, rn, new,
zebra_route_string(new->type),
old,
zebra_route_string(old->type));
else
zlog_debug(
"%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive",
zvrf_name(zvrf), zvrf_id(zvrf),
new->table, rn, rn, new,
zebra_route_string(new->type));
}
/*
* When we have gotten to this point
* the new route entry has no nexthops
* that are usable and as such we need
* to remove the old route, but only
* if we were the one who installed
* the old route
*/
if (!RIB_SYSTEM_ROUTE(old)) {
/* If labeled-unicast route, uninstall transit
* LSP. */
if (zebra_rib_labeled_unicast(old))
zebra_mpls_lsp_uninstall(zvrf, rn, old);
rib_uninstall_kernel(rn, old);
}
}
} else {
/*
* Same route selected; check if in the FIB and if not,
* re-install. This is housekeeping code to deal with
* race conditions in kernel with linux netlink reporting
* interface up before IPv4 or IPv6 protocol is ready
* to add routes.
*/
if (!CHECK_FLAG(new->status, ROUTE_ENTRY_INSTALLED) ||
RIB_SYSTEM_ROUTE(new))
rib_install_kernel(rn, new, NULL);
}
/* Update prior route. */
if (new != old)
UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
/* Clear changed flag. */
UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
}
/* Check if 'alternate' RIB entry is better than 'current'. */
static struct route_entry *rib_choose_best(struct route_entry *current,
struct route_entry *alternate)
{
if (current == NULL)
return alternate;
/* filter route selection in following order:
* - connected beats other types
* - if both connected, loopback or vrf wins
* - lower distance beats higher
* - lower metric beats higher for equal distance
* - last, hence oldest, route wins tie break.
*/
/* Connected routes. Check to see if either are a vrf
* or loopback interface. If not, pick the last connected
* route of the set of lowest metric connected routes.
*/
if (alternate->type == ZEBRA_ROUTE_CONNECT) {
if (current->type != ZEBRA_ROUTE_CONNECT)
return alternate;
/* both are connected. are either loop or vrf? */
struct nexthop *nexthop = NULL;
for (ALL_NEXTHOPS(alternate->nhe->nhg, nexthop)) {
struct interface *ifp = if_lookup_by_index(
nexthop->ifindex, alternate->vrf_id);
if (ifp && if_is_loopback(ifp))
return alternate;
}
for (ALL_NEXTHOPS(current->nhe->nhg, nexthop)) {
struct interface *ifp = if_lookup_by_index(
nexthop->ifindex, current->vrf_id);
if (ifp && if_is_loopback(ifp))
return current;
}
/* Neither are loop or vrf so pick best metric */
if (alternate->metric <= current->metric)
return alternate;
return current;
}
if (current->type == ZEBRA_ROUTE_CONNECT)
return current;
/* higher distance loses */
if (alternate->distance < current->distance)
return alternate;
if (current->distance < alternate->distance)
return current;
/* metric tie-breaks equal distance */
if (alternate->metric <= current->metric)
return alternate;
return current;
}
/* Core function for processing routing information base. */
static void rib_process(struct route_node *rn)
{
struct route_entry *re;
struct route_entry *next;
struct route_entry *old_selected = NULL;
struct route_entry *new_selected = NULL;
struct route_entry *old_fib = NULL;
struct route_entry *new_fib = NULL;
struct route_entry *best = NULL;
rib_dest_t *dest;
struct zebra_vrf *zvrf = NULL;
struct vrf *vrf;
vrf_id_t vrf_id = VRF_UNKNOWN;
assert(rn);
dest = rib_dest_from_rnode(rn);
/*
* We have an enqueued node with nothing to process here
* let's just finish up and return;
*/
if (!dest)
return;
zvrf = rib_dest_vrf(dest);
vrf_id = zvrf_id(zvrf);
vrf = vrf_lookup_by_id(vrf_id);
/*
* we can have rn's that have a NULL info pointer
* (dest). As such let's not let the deref happen
* additionally we know RNODE_FOREACH_RE_SAFE
* will not iterate so we are ok.
*/
if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
struct route_entry *re = re_list_first(&dest->routes);
zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
VRF_LOGNAME(vrf), vrf_id, re->table, rn,
rn);
}
old_fib = dest->selected_fib;
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
char flags_buf[128];
char status_buf[128];
zlog_debug(
"%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
VRF_LOGNAME(vrf), vrf_id, re->table, rn, re,
zebra_route_string(re->type),
_dump_re_status(re, status_buf,
sizeof(status_buf)),
zclient_dump_route_flags(re->flags, flags_buf,
sizeof(flags_buf)),
re->distance, re->metric);
}
/* Currently selected re. */
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
assert(old_selected == NULL);
old_selected = re;
}
/* Skip deleted entries from selection */
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
/*
* If the route entry has changed, verify/resolve
* the nexthops associated with the entry.
*
* In any event if we have nexthops that are not active
* then we cannot use this particular route entry so
* skip it.
*/
if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
if (!nexthop_active_update(rn, re)) {
const struct prefix *p;
struct rib_table_info *info;
if (re->type == ZEBRA_ROUTE_TABLE) {
/* XXX: HERE BE DRAGONS!!!!!
* In all honesty, I have not yet
* figured out what this part does or
* why the ROUTE_ENTRY_CHANGED test
* above is correct or why we need to
* delete a route here, and also not
* whether this concerns both selected
* and fib route, or only selected
* or only fib
*
* This entry was denied by the 'ip
* protocol
* table' route-map, we need to delete
* it */
if (re != old_selected) {
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
__func__,
VRF_LOGNAME(
vrf),
vrf_id, rn);
rib_unlink(rn, re);
} else
SET_FLAG(re->status,
ROUTE_ENTRY_REMOVED);
}
info = srcdest_rnode_table_info(rn);
srcdest_rnode_prefixes(rn, &p, NULL);
zsend_route_notify_owner(
rn, re, ZAPI_ROUTE_FAIL_INSTALL,
info->afi, info->safi);
continue;
}
} else {
/*
* If the re has not changed and the nhg we have is
* not usable, then we cannot use this route entry
* for consideration, as that the route will just
* not install if it is selected.
*/
if (!nexthop_group_active_nexthop_num(&re->nhe->nhg))
continue;
}
/* Infinite distance. */
if (re->distance == DISTANCE_INFINITY &&
re->type != ZEBRA_ROUTE_KERNEL) {
UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
continue;
}
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
best = rib_choose_best(new_fib, re);
if (new_fib && best != new_fib)
UNSET_FLAG(new_fib->status,
ROUTE_ENTRY_CHANGED);
new_fib = best;
} else {
best = rib_choose_best(new_selected, re);
if (new_selected && best != new_selected)
UNSET_FLAG(new_selected->status,
ROUTE_ENTRY_CHANGED);
new_selected = best;
}
if (best != re)
UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
} /* RNODE_FOREACH_RE */
/* If no FIB override route, use the selected route also for FIB */
if (new_fib == NULL)
new_fib = new_selected;
/* After the cycle is finished, the following pointers will be set:
* old_selected --- RE entry currently having SELECTED
* new_selected --- RE entry that is newly SELECTED
* old_fib --- RE entry currently in kernel FIB
* new_fib --- RE entry that is newly to be in kernel FIB
*
* new_selected will get SELECTED flag, and is going to be redistributed
* the zclients. new_fib (which can be new_selected) will be installed
* in kernel.
*/
if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
struct route_entry *entry;
entry = old_selected
? old_selected
: new_selected
? new_selected
: old_fib ? old_fib
: new_fib ? new_fib : NULL;
zlog_debug(
"%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
VRF_LOGNAME(vrf), vrf_id, entry ? entry->table : 0, rn,
(void *)old_selected, (void *)new_selected,
(void *)old_fib, (void *)new_fib);
}
/* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
* fib == selected */
bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
ROUTE_ENTRY_CHANGED);
/* Update SELECTED entry */
if (old_selected != new_selected || selected_changed) {
if (new_selected && new_selected != new_fib)
UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
if (new_selected)
SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
if (old_selected) {
/*
* If we're removing the old entry, we should tell
* redist subscribers about that *if* they aren't
* going to see a redist for the new entry.
*/
if (!new_selected || CHECK_FLAG(old_selected->status,
ROUTE_ENTRY_REMOVED))
redistribute_delete(rn, old_selected,
new_selected);
if (old_selected != new_selected)
UNSET_FLAG(old_selected->flags,
ZEBRA_FLAG_SELECTED);
}
}
/* Update fib according to selection results */
if (new_fib && old_fib)
rib_process_update_fib(zvrf, rn, old_fib, new_fib);
else if (new_fib)
rib_process_add_fib(zvrf, rn, new_fib);
else if (old_fib)
rib_process_del_fib(zvrf, rn, old_fib);
/* Remove all RE entries queued for removal */
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
if (IS_ZEBRA_DEBUG_RIB) {
rnode_debug(rn, vrf_id, "rn %p, removing re %p",
(void *)rn, (void *)re);
}
rib_unlink(rn, re);
}
}
/*
* Check if the dest can be deleted now.
*/
rib_gc_dest(rn);
}
static void zebra_rib_evaluate_mpls(struct route_node *rn)
{
rib_dest_t *dest = rib_dest_from_rnode(rn);
struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
if (!dest)
return;
if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug(
"%s(%u): Scheduling all LSPs upon RIB completion",
zvrf_name(zvrf), zvrf_id(zvrf));
zebra_mpls_lsp_schedule(zvrf);
mpls_unmark_lsps_for_processing(rn);
}
}
/*
* Utility to match route with dplane context data
*/
static bool rib_route_match_ctx(const struct route_entry *re,
const struct zebra_dplane_ctx *ctx,
bool is_update)
{
bool result = false;
if (is_update) {
/*
* In 'update' case, we test info about the 'previous' or
* 'old' route
*/
if ((re->type == dplane_ctx_get_old_type(ctx)) &&
(re->instance == dplane_ctx_get_old_instance(ctx))) {
result = true;
/* We use an extra test for statics, and another for
* kernel routes.
*/
if (re->type == ZEBRA_ROUTE_STATIC &&
(re->distance != dplane_ctx_get_old_distance(ctx) ||
re->tag != dplane_ctx_get_old_tag(ctx))) {
result = false;
} else if (re->type == ZEBRA_ROUTE_KERNEL &&
re->metric !=
dplane_ctx_get_old_metric(ctx)) {
result = false;
}
}
} else {
/*
* Ordinary, single-route case using primary context info
*/
if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
/* Skip route that's been deleted */
goto done;
}
if ((re->type == dplane_ctx_get_type(ctx)) &&
(re->instance == dplane_ctx_get_instance(ctx))) {
result = true;
/* We use an extra test for statics, and another for
* kernel routes.
*/
if (re->type == ZEBRA_ROUTE_STATIC &&
(re->distance != dplane_ctx_get_distance(ctx) ||
re->tag != dplane_ctx_get_tag(ctx))) {
result = false;
} else if (re->type == ZEBRA_ROUTE_KERNEL &&
re->metric != dplane_ctx_get_metric(ctx)) {
result = false;
} else if (re->type == ZEBRA_ROUTE_CONNECT) {
result = nexthop_group_equal_no_recurse(
&re->nhe->nhg, dplane_ctx_get_ng(ctx));
}
}
}
done:
return (result);
}
static void zebra_rib_fixup_system(struct route_node *rn)
{
struct route_entry *re;
RNODE_FOREACH_RE(rn, re) {
struct nexthop *nhop;
if (!RIB_SYSTEM_ROUTE(re))
continue;
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
for (ALL_NEXTHOPS(re->nhe->nhg, nhop)) {
if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
continue;
SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
}
}
}
/* Route comparison logic, with various special cases. */
static bool rib_compare_routes(const struct route_entry *re1,
const struct route_entry *re2)
{
if (re1->type != re2->type)
return false;
if (re1->instance != re2->instance)
return false;
if (re1->type == ZEBRA_ROUTE_KERNEL && re1->metric != re2->metric)
return false;
if (CHECK_FLAG(re1->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
re1->distance != re2->distance)
return false;
/* We support multiple connected routes: this supports multiple
* v6 link-locals, and we also support multiple addresses in the same
* subnet on a single interface.
*/
if (re1->type != ZEBRA_ROUTE_CONNECT)
return true;
return false;
}
/*
* Compare nexthop lists from a route and a dplane context; test whether
* the list installed in the FIB matches the route's list.
* Set 'changed_p' to 'true' if there were changes to the route's
* installed nexthops.
*
* Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
* list.
*/
static bool rib_update_nhg_from_ctx(struct nexthop_group *re_nhg,
const struct nexthop_group *ctx_nhg,
bool *changed_p)
{
bool matched_p = true;
struct nexthop *nexthop, *ctx_nexthop;
/* Get the first `installed` one to check against.
* If the dataplane doesn't set these to be what was actually installed,
* it will just be whatever was in re->nhe->nhg?
*/
ctx_nexthop = ctx_nhg->nexthop;
if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
|| !CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_ACTIVE))
ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
for (ALL_NEXTHOPS_PTR(re_nhg, nexthop)) {
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
continue;
if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
continue;
/* Check for a FIB nexthop corresponding to the RIB nexthop */
if (!nexthop_same(ctx_nexthop, nexthop)) {
/* If the FIB doesn't know about the nexthop,
* it's not installed
*/
if (IS_ZEBRA_DEBUG_RIB_DETAILED ||
IS_ZEBRA_DEBUG_NHG_DETAIL) {
zlog_debug("%s: no ctx match for rib nh %pNHv %s",
__func__, nexthop,
(CHECK_FLAG(nexthop->flags,
NEXTHOP_FLAG_FIB) ?
"(FIB)":""));
}
matched_p = false;
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
*changed_p = true;
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
/* Keep checking nexthops */
continue;
}
if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) {
if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: rib nh %pNHv -> installed",
__func__, nexthop);
*changed_p = true;
}
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
} else {
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: rib nh %pNHv -> uninstalled",
__func__, nexthop);
*changed_p = true;
}
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
}
ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
}
return matched_p;
}
/*
* Update a route from a dplane context. This consolidates common code
* that can be used in processing of results from FIB updates, and in
* async notification processing.
* The return is 'true' if the installed nexthops changed; 'false' otherwise.
*/
static bool rib_update_re_from_ctx(struct route_entry *re,
struct route_node *rn,
struct zebra_dplane_ctx *ctx)
{
struct nexthop *nexthop;
bool matched;
const struct nexthop_group *ctxnhg;
struct nexthop_group *re_nhg;
bool is_selected = false; /* Is 're' currently the selected re? */
bool changed_p = false; /* Change to nexthops? */
rib_dest_t *dest;
struct vrf *vrf;
vrf = vrf_lookup_by_id(re->vrf_id);
dest = rib_dest_from_rnode(rn);
if (dest)
is_selected = (re == dest->selected_fib);
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
(is_selected ? "" : "NOT "), re);
/* Update zebra's nexthop FIB flag for each nexthop that was installed.
* If the installed set differs from the set requested by the rib/owner,
* we use the fib-specific nexthop-group to record the actual FIB
* status.
*/
matched = false;
ctxnhg = dplane_ctx_get_ng(ctx);
/* Check route's fib group and incoming notif group for equivalence.
*
* Let's assume the nexthops are ordered here to save time.
*/
/* TODO -- this isn't testing or comparing the FIB flags; we should
* do a more explicit loop, checking the incoming notification's flags.
*/
if (re->fib_ng.nexthop && ctxnhg->nexthop &&
nexthop_group_equal(&re->fib_ng, ctxnhg))
matched = true;
/* If the new FIB set matches the existing FIB set, we're done. */
if (matched) {
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
goto check_backups;
} else if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG)) {
/*
* Free stale fib list and move on to check the rib nhg.
*/
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
nexthops_free(re->fib_ng.nexthop);
re->fib_ng.nexthop = NULL;
UNSET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
/* Note that the installed nexthops have changed */
changed_p = true;
} else {
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
}
/*
* Compare with the rib nexthop group. The comparison here is different:
* the RIB group may be a superset of the list installed in the FIB. We
* walk the RIB group, looking for the 'installable' candidate
* nexthops, and then check those against the set
* that is actually installed.
*
* Assume nexthops are ordered here as well.
*/
/* If nothing is installed, we can skip some of the checking/comparison
* of nexthops.
*/
if (ctxnhg->nexthop == NULL) {
changed_p = true;
goto no_nexthops;
}
matched = rib_update_nhg_from_ctx(&(re->nhe->nhg), ctxnhg, &changed_p);
/* If all nexthops were processed, we're done */
if (matched) {
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
(changed_p ? "true" : "false"));
goto check_backups;
}
no_nexthops:
/* FIB nexthop set differs from the RIB set:
* create a fib-specific nexthop-group
*/
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
(changed_p ? "true" : "false"),
ctxnhg->nexthop != NULL ? "" : " (empty)");
/* Set the flag about the dedicated fib list */
SET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
if (ctxnhg->nexthop)
copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop, NULL);
check_backups:
/*
* Check the status of the route's backup nexthops, if any.
* The logic for backups is somewhat different: if any backup is
* installed, a new fib nhg will be attached to the route.
*/
re_nhg = zebra_nhg_get_backup_nhg(re->nhe);
if (re_nhg == NULL)
goto done; /* No backup nexthops */
/* First check the route's 'fib' list of backups, if it's present
* from some previous event.
*/
re_nhg = &re->fib_backup_ng;
ctxnhg = dplane_ctx_get_backup_ng(ctx);
matched = false;
if (re_nhg->nexthop && ctxnhg && nexthop_group_equal(re_nhg, ctxnhg))
matched = true;
/* If the new FIB set matches an existing FIB set, we're done. */
if (matched) {
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
VRF_LOGNAME(vrf), re->vrf_id, rn);
goto done;
} else if (re->fib_backup_ng.nexthop) {
/*
* Free stale fib backup list and move on to check
* the route's backups.
*/
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
VRF_LOGNAME(vrf), re->vrf_id, rn);
nexthops_free(re->fib_backup_ng.nexthop);
re->fib_backup_ng.nexthop = NULL;
/* Note that the installed nexthops have changed */
changed_p = true;
} else {
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u):%pRN update_from_ctx(): no fib backup nhg",
VRF_LOGNAME(vrf), re->vrf_id, rn);
}
/*
* If a FIB backup nexthop set exists, attach a copy
* to the route if any backup is installed
*/
if (ctxnhg && ctxnhg->nexthop) {
for (ALL_NEXTHOPS_PTR(ctxnhg, nexthop)) {
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
break;
}
/* If no installed backups, we're done */
if (nexthop == NULL)
goto done;
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
VRF_LOGNAME(vrf), re->vrf_id, rn,
(changed_p ? "true" : "false"));
copy_nexthops(&(re->fib_backup_ng.nexthop), ctxnhg->nexthop,
NULL);
}
done:
return changed_p;
}
/*
* Helper to locate a zebra route-node from a dplane context. This is used
* when processing dplane results, e.g. Note well: the route-node is returned
* with a ref held - route_unlock_node() must be called eventually.
*/
struct route_node *rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx)
{
struct route_table *table = NULL;
struct route_node *rn = NULL;
const struct prefix *dest_pfx, *src_pfx;
/* Locate rn and re(s) from ctx */
table = zebra_vrf_lookup_table_with_table_id(
dplane_ctx_get_afi(ctx), dplane_ctx_get_safi(ctx),
dplane_ctx_get_vrf(ctx), dplane_ctx_get_table(ctx));
if (table == NULL) {
if (IS_ZEBRA_DEBUG_DPLANE) {
zlog_debug(
"Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
dplane_ctx_get_afi(ctx),
dplane_ctx_get_safi(ctx),
vrf_id_to_name(dplane_ctx_get_vrf(ctx)),
dplane_ctx_get_vrf(ctx));
}
goto done;
}
dest_pfx = dplane_ctx_get_dest(ctx);
src_pfx = dplane_ctx_get_src(ctx);
rn = srcdest_rnode_get(table, dest_pfx,
src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
done:
return rn;
}
/*
* Route-update results processing after async dataplane update.
*/
static void rib_process_result(struct zebra_dplane_ctx *ctx)
{
struct zebra_vrf *zvrf = NULL;
struct vrf *vrf;
struct route_node *rn = NULL;
struct route_entry *re = NULL, *old_re = NULL, *rib;
bool is_update = false;
enum dplane_op_e op;
enum zebra_dplane_result status;
uint32_t seq;
rib_dest_t *dest;
bool fib_changed = false;
struct rib_table_info *info;
bool rt_delete = false;
zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx));
vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
/* Locate rn and re(s) from ctx */
rn = rib_find_rn_from_ctx(ctx);
if (rn == NULL) {
if (IS_ZEBRA_DEBUG_DPLANE) {
zlog_debug(
"Failed to process dplane results: no route for %s(%u):%pRN",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), rn);
}
goto done;
}
dest = rib_dest_from_rnode(rn);
info = srcdest_rnode_table_info(rn);
op = dplane_ctx_get_op(ctx);
status = dplane_ctx_get_status(ctx);
if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
zlog_debug(
"%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn, ctx, dplane_op2str(op),
dplane_res2str(status));
/*
* Update is a bit of a special case, where we may have both old and new
* routes to post-process.
*/
is_update = dplane_ctx_is_update(ctx);
/*
* Take a pass through the routes, look for matches with the context
* info.
*/
RNODE_FOREACH_RE(rn, rib) {
if (re == NULL) {
if (rib_route_match_ctx(rib, ctx, false))
re = rib;
}
/* Check for old route match */
if (is_update && (old_re == NULL)) {
if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
old_re = rib;
}
/* Have we found the routes we need to work on? */
if (re && ((!is_update || old_re)))
break;
}
seq = dplane_ctx_get_seq(ctx);
/*
* Check sequence number(s) to detect stale results before continuing
*/
if (re) {
if (re->dplane_sequence != seq) {
if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
zlog_debug(
"%s(%u):%pRN Stale dplane result for re %p",
VRF_LOGNAME(vrf),
dplane_ctx_get_vrf(ctx), rn, re);
} else {
if (!zrouter.asic_offloaded ||
(CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED) ||
CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED)))
UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
}
}
if (old_re) {
if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
zlog_debug(
"%s(%u:%u):%pRN Stale dplane result for old_re %p",
VRF_LOGNAME(vrf),
dplane_ctx_get_vrf(ctx), old_re->table,
rn, old_re);
} else
UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
}
switch (op) {
case DPLANE_OP_ROUTE_INSTALL:
case DPLANE_OP_ROUTE_UPDATE:
if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
if (re) {
UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
}
/*
* On an update operation from the same route type
* context retrieval currently has no way to know
* which was the old and which was the new.
* So don't unset our flags that we just set.
* We know redistribution is ok because the
* old_re in this case is used for nothing
* more than knowing whom to contact if necessary.
*/
if (old_re && old_re != re) {
UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
UNSET_FLAG(old_re->status,
ROUTE_ENTRY_INSTALLED);
}
/* Update zebra route based on the results in
* the context struct.
*/
if (re) {
fib_changed =
rib_update_re_from_ctx(re, rn, ctx);
if (!fib_changed) {
if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
zlog_debug(
"%s(%u:%u):%pRN no fib change for re",
VRF_LOGNAME(vrf),
dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(
ctx),
rn);
}
/* Redistribute if this is the selected re */
if (dest && re == dest->selected_fib)
redistribute_update(rn, re, old_re);
}
/*
* System routes are weird in that they
* allow multiple to be installed that match
* to the same prefix, so after we get the
* result we need to clean them up so that
* we can actually use them.
*/
if ((re && RIB_SYSTEM_ROUTE(re)) ||
(old_re && RIB_SYSTEM_ROUTE(old_re)))
zebra_rib_fixup_system(rn);
if (zvrf)
zvrf->installs++;
/* Notify route owner */
if (zebra_router_notify_on_ack())
zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
else {
if (re) {
if (CHECK_FLAG(re->flags,
ZEBRA_FLAG_OFFLOADED))
zsend_route_notify_owner_ctx(
ctx,
ZAPI_ROUTE_INSTALLED);
if (CHECK_FLAG(
re->flags,
ZEBRA_FLAG_OFFLOAD_FAILED))
zsend_route_notify_owner_ctx(
ctx,
ZAPI_ROUTE_FAIL_INSTALL);
}
}
} else {
if (re) {
SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
} if (old_re)
SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
if (re)
zsend_route_notify_owner(
rn, re, ZAPI_ROUTE_FAIL_INSTALL,
info->afi, info->safi);
zlog_warn("%s(%u:%u):%pRN: Route install failed",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn);
}
break;
case DPLANE_OP_ROUTE_DELETE:
rt_delete = true;
if (re)
SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
/*
* In the delete case, the zebra core datastructs were
* updated (or removed) at the time the delete was issued,
* so we're just notifying the route owner.
*/
if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
if (re) {
UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
}
zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
if (zvrf)
zvrf->removals++;
} else {
if (re)
SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
zsend_route_notify_owner_ctx(ctx,
ZAPI_ROUTE_REMOVE_FAIL);
zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn);
}
/*
* System routes are weird in that they
* allow multiple to be installed that match
* to the same prefix, so after we get the
* result we need to clean them up so that
* we can actually use them.
*/
if ((re && RIB_SYSTEM_ROUTE(re)) ||
(old_re && RIB_SYSTEM_ROUTE(old_re)))
zebra_rib_fixup_system(rn);
break;
default:
break;
}
zebra_rib_evaluate_rn_nexthops(rn, seq, rt_delete);
zebra_rib_evaluate_mpls(rn);
done:
if (rn)
route_unlock_node(rn);
}
/*
* Count installed/FIB nexthops
*/
static int rib_count_installed_nh(struct route_entry *re)
{
int count = 0;
struct nexthop *nexthop;
struct nexthop_group *nhg;
nhg = rib_get_fib_nhg(re);
for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
/* The meaningful flag depends on where the installed
* nexthops reside.
*/
if (nhg == &(re->fib_ng)) {
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
count++;
} else {
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
count++;
}
}
nhg = rib_get_fib_backup_nhg(re);
if (nhg) {
for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
count++;
}
}
return count;
}
/*
* Handle notification from async dataplane: the dataplane has detected
* some change to a route, and notifies zebra so that the control plane
* can reflect that change.
*/
static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx)
{
struct route_node *rn = NULL;
struct route_entry *re = NULL;
struct vrf *vrf;
struct nexthop *nexthop;
rib_dest_t *dest;
bool fib_changed = false;
bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB;
int start_count, end_count;
vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
/* Locate rn and re(s) from ctx */
rn = rib_find_rn_from_ctx(ctx);
if (rn == NULL) {
if (debug_p) {
zlog_debug(
"Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn);
}
goto done;
}
dest = rib_dest_from_rnode(rn);
if (debug_p)
zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn, ctx);
/*
* Take a pass through the routes, look for matches with the context
* info.
*/
RNODE_FOREACH_RE(rn, re) {
if (rib_route_match_ctx(re, ctx, false /*!update*/))
break;
}
/* No match? Nothing we can do */
if (re == NULL) {
if (debug_p)
zlog_debug(
"%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn,
zebra_route_string(dplane_ctx_get_type(ctx)));
goto done;
}
/* Ensure we clear the QUEUED flag */
if (!zrouter.asic_offloaded)
UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
/* Is this a notification that ... matters? We mostly care about
* the route that is currently selected for installation; we may also
* get an un-install notification, and handle that too.
*/
if (re != dest->selected_fib) {
/*
* If we need to, clean up after a delete that was part of
* an update operation.
*/
end_count = 0;
for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
end_count++;
}
/* If no nexthops or none installed, ensure that this re
* gets its 'installed' flag cleared.
*/
if (end_count == 0) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
if (debug_p)
zlog_debug(
"%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
VRF_LOGNAME(vrf),
dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn,
zebra_route_string(
dplane_ctx_get_type(ctx)));
} else {
/* At least report on the event. */
if (debug_p)
zlog_debug(
"%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
VRF_LOGNAME(vrf),
dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn,
zebra_route_string(
dplane_ctx_get_type(ctx)));
}
goto done;
}
/* We'll want to determine whether the installation status of the
* route has changed: we'll check the status before processing,
* and then again if there's been a change.
*/
start_count = 0;
if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
start_count = rib_count_installed_nh(re);
/* Update zebra's nexthop FIB flags based on the context struct's
* nexthops.
*/
fib_changed = rib_update_re_from_ctx(re, rn, ctx);
if (!fib_changed) {
if (debug_p)
zlog_debug(
"%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn);
}
/*
* Perform follow-up work if the actual status of the prefix
* changed.
*/
end_count = rib_count_installed_nh(re);
/* Various fib transitions: changed nexthops; from installed to
* not-installed; or not-installed to installed.
*/
if (start_count > 0 && end_count > 0) {
if (debug_p)
zlog_debug(
"%s(%u:%u):%pRN applied nexthop changes from dplane notification",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn);
/* Changed nexthops - update kernel/others */
dplane_route_notif_update(rn, re,
DPLANE_OP_ROUTE_UPDATE, ctx);
} else if (start_count == 0 && end_count > 0) {
if (debug_p)
zlog_debug(
"%s(%u:%u):%pRN installed transition from dplane notification",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn);
/* We expect this to be the selected route, so we want
* to tell others about this transition.
*/
SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
/* Changed nexthops - update kernel/others */
dplane_route_notif_update(rn, re, DPLANE_OP_ROUTE_UPDATE, ctx);
/* Redistribute, lsp, and nht update */
redistribute_update(rn, re, NULL);
} else if (start_count > 0 && end_count == 0) {
if (debug_p)
zlog_debug(
"%s(%u:%u):%pRN un-installed transition from dplane notification",
VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx), rn);
/* Transition from _something_ installed to _nothing_
* installed.
*/
/* We expect this to be the selected route, so we want
* to tell others about this transistion.
*/
UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
/* Changed nexthops - update kernel/others */
dplane_route_notif_update(rn, re, DPLANE_OP_ROUTE_DELETE, ctx);
/* Redistribute, lsp, and nht update */
redistribute_delete(rn, re, NULL);
}
/* Make any changes visible for lsp and nexthop-tracking processing */
zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
false);
zebra_rib_evaluate_mpls(rn);
done:
if (rn)
route_unlock_node(rn);
}
/*
* Process a node from the EVPN/VXLAN subqueue.
*/
static void process_subq_evpn(struct listnode *lnode)
{
struct wq_evpn_wrapper *w;
/* In general, the list node points to a wrapper object
* holding the info necessary to make some update.
*/
w = listgetdata(lnode);
if (!w)
return;
if (w->type == WQ_EVPN_WRAPPER_TYPE_VRFROUTE) {
if (w->add_p)
zebra_vxlan_evpn_vrf_route_add(w->vrf_id, &w->macaddr,
&w->ip, &w->prefix);
else
zebra_vxlan_evpn_vrf_route_del(w->vrf_id, &w->ip,
&w->prefix);
} else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_ES) {
if (w->add_p)
zebra_evpn_remote_es_add(&w->esi, w->ip.ipaddr_v4,
w->esr_rxed, w->df_alg,
w->df_pref);
else
zebra_evpn_remote_es_del(&w->esi, w->ip.ipaddr_v4);
} else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_MACIP) {
uint16_t ipa_len = 0;
if (w->ip.ipa_type == IPADDR_V4)
ipa_len = IPV4_MAX_BYTELEN;
else if (w->ip.ipa_type == IPADDR_V6)
ipa_len = IPV6_MAX_BYTELEN;
if (w->add_p)
zebra_evpn_rem_macip_add(w->vni, &w->macaddr, ipa_len,
&w->ip, w->flags, w->seq,
w->vtep_ip, &w->esi);
else
zebra_evpn_rem_macip_del(w->vni, &w->macaddr, ipa_len,
&w->ip, w->vtep_ip);
} else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_VTEP) {
if (w->add_p)
zebra_vxlan_remote_vtep_add(w->vrf_id, w->vni,
w->vtep_ip, w->flags);
else
zebra_vxlan_remote_vtep_del(w->vrf_id, w->vni,
w->vtep_ip);
}
XFREE(MTYPE_WQ_WRAPPER, w);
}
/*
* Process the nexthop-group workqueue subqueue
*/
static void process_subq_nhg(struct listnode *lnode)
{
struct nhg_ctx *ctx;
struct nhg_hash_entry *nhe, *newnhe;
struct wq_nhg_wrapper *w;
uint8_t qindex = META_QUEUE_NHG;
w = listgetdata(lnode);
if (!w)
return;
/* Two types of object - an update from the local kernel, or
* an nhg update from a daemon.
*/
if (w->type == WQ_NHG_WRAPPER_TYPE_CTX) {
ctx = w->u.ctx;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"NHG Context id=%u dequeued from sub-queue %s",
ctx->id, subqueue2str(qindex));
/* Process nexthop group updates coming 'up' from the OS */
nhg_ctx_process(ctx);
} else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG) {
nhe = w->u.nhe;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("NHG %u dequeued from sub-queue %s", nhe->id,
subqueue2str(qindex));
/* Process incoming nhg update, probably from a proto daemon */
newnhe = zebra_nhg_proto_add(nhe->id, nhe->type,
nhe->zapi_instance,
nhe->zapi_session, &nhe->nhg, 0);
/* Report error to daemon via ZAPI */
if (newnhe == NULL)
zsend_nhg_notify(nhe->type, nhe->zapi_instance,
nhe->zapi_session, nhe->id,
ZAPI_NHG_FAIL_INSTALL);
/* Free temp nhe - we own that memory. */
zebra_nhg_free(nhe);
}
XFREE(MTYPE_WQ_WRAPPER, w);
}
static void process_subq_early_label(struct listnode *lnode)
{
struct wq_label_wrapper *w = listgetdata(lnode);
struct zebra_vrf *zvrf;
if (!w)
return;
zvrf = vrf_info_lookup(w->vrf_id);
if (!zvrf) {
XFREE(MTYPE_WQ_WRAPPER, w);
return;
}
switch (w->type) {
case WQ_LABEL_FTN_UNINSTALL:
zebra_mpls_ftn_uninstall(zvrf, w->ltype, &w->p, w->route_type,
w->route_instance);
break;
case WQ_LABEL_LABELS_PROCESS:
zebra_mpls_zapi_labels_process(w->add_p, zvrf, &w->zl);
break;
}
XFREE(MTYPE_WQ_WRAPPER, w);
}
static void process_subq_route(struct listnode *lnode, uint8_t qindex)
{
struct route_node *rnode = NULL;
rib_dest_t *dest = NULL;
struct zebra_vrf *zvrf = NULL;
rnode = listgetdata(lnode);
dest = rib_dest_from_rnode(rnode);
assert(dest);
zvrf = rib_dest_vrf(dest);
rib_process(rnode);
if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
struct route_entry *re = NULL;
/*
* rib_process may have freed the dest
* as part of the garbage collection. Let's
* prevent stupidity from happening.
*/
dest = rib_dest_from_rnode(rnode);
if (dest)
re = re_list_first(&dest->routes);
zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
zvrf_name(zvrf), zvrf_id(zvrf), re ? re->table : 0,
rnode, rnode, subqueue2str(qindex));
}
if (rnode->info)
UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
RIB_ROUTE_QUEUED(qindex));
route_unlock_node(rnode);
}
static void rib_re_nhg_free(struct route_entry *re)
{
if (re->nhe && re->nhe_id) {
assert(re->nhe->id == re->nhe_id);
route_entry_update_nhe(re, NULL);
} else if (re->nhe && re->nhe->nhg.nexthop)
nexthops_free(re->nhe->nhg.nexthop);
nexthops_free(re->fib_ng.nexthop);
}
struct zebra_early_route {
afi_t afi;
safi_t safi;
struct prefix p;
struct prefix_ipv6 src_p;
bool src_p_provided;
struct route_entry *re;
struct nhg_hash_entry *re_nhe;
bool startup;
bool deletion;
bool fromkernel;
};
static void early_route_memory_free(struct zebra_early_route *ere)
{
if (ere->re_nhe)
zebra_nhg_free(ere->re_nhe);
XFREE(MTYPE_RE, ere->re);
XFREE(MTYPE_WQ_WRAPPER, ere);
}
static void process_subq_early_route_add(struct zebra_early_route *ere)
{
struct route_entry *re = ere->re;
struct route_table *table;
struct nhg_hash_entry *nhe = NULL;
struct route_node *rn;
struct route_entry *same = NULL, *first_same = NULL;
int same_count = 0;
rib_dest_t *dest;
/* Lookup table. */
table = zebra_vrf_get_table_with_table_id(ere->afi, ere->safi,
re->vrf_id, re->table);
if (!table) {
early_route_memory_free(ere);
return;
}
if (re->nhe_id > 0) {
nhe = zebra_nhg_lookup_id(re->nhe_id);
if (!nhe) {
/*
* We've received from the kernel a nexthop id
* that we don't have saved yet. More than likely
* it has not been processed and is on the
* queue to be processed. Let's stop what we
* are doing and cause the meta q to be processed
* storing this for later.
*
* This is being done this way because zebra
* runs with the assumption t
*/
flog_err(
EC_ZEBRA_TABLE_LOOKUP_FAILED,
"Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
re->nhe_id, &ere->p);
early_route_memory_free(ere);
return;
}
} else {
/* Lookup nhe from route information */
nhe = zebra_nhg_rib_find_nhe(ere->re_nhe, ere->afi);
if (!nhe) {
char buf2[PREFIX_STRLEN] = "";
flog_err(
EC_ZEBRA_TABLE_LOOKUP_FAILED,
"Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
&ere->p, ere->src_p_provided ? " from " : "",
ere->src_p_provided
? prefix2str(&ere->src_p, buf2,
sizeof(buf2))
: "");
early_route_memory_free(ere);
return;
}
}
/*
* Attach the re to the nhe's nexthop group.
*
* TODO: This will need to change when we start getting IDs from upper
* level protocols, as the refcnt might be wrong, since it checks
* if old_id != new_id.
*/
route_entry_update_nhe(re, nhe);
/* Make it sure prefixlen is applied to the prefix. */
apply_mask(&ere->p);
if (ere->src_p_provided)
apply_mask_ipv6(&ere->src_p);
/* Set default distance by route type. */
if (re->distance == 0)
re->distance = route_distance(re->type);
/* Lookup route node.*/
rn = srcdest_rnode_get(table, &ere->p,
ere->src_p_provided ? &ere->src_p : NULL);
/*
* If same type of route are installed, treat it as a implicit
* withdraw. If the user has specified the No route replace semantics
* for the install don't do a route replace.
*/
RNODE_FOREACH_RE (rn, same) {
if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED)) {
same_count++;
continue;
}
/* Compare various route_entry properties */
if (rib_compare_routes(re, same)) {
same_count++;
if (first_same == NULL)
first_same = same;
}
}
same = first_same;
if (!ere->startup && (re->flags & ZEBRA_FLAG_SELFROUTE) &&
zrouter.asic_offloaded) {
if (!same) {
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
rn);
/*
* We are not on startup, this is a self route
* and we have asic offload. Which means
* we are getting a callback for a entry
* that was already deleted to the kernel
* but an earlier response was just handed
* back. Drop it on the floor
*/
early_route_memory_free(ere);
return;
}
}
/* If this route is kernel/connected route, notify the dataplane. */
if (RIB_SYSTEM_ROUTE(re)) {
/* Notify dataplane */
dplane_sys_route_add(rn, re);
}
/* Link new re to node.*/
if (IS_ZEBRA_DEBUG_RIB) {
rnode_debug(
rn, re->vrf_id,
"Inserting route rn %p, re %p (%s) existing %p, same_count %d",
rn, re, zebra_route_string(re->type), same, same_count);
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
route_entry_dump(
&ere->p,
ere->src_p_provided ? &ere->src_p : NULL, re);
}
SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
rib_addnode(rn, re, 1);
/* Free implicit route.*/
if (same)
rib_delnode(rn, same);
/* See if we can remove some RE entries that are queued for
* removal, but won't be considered in rib processing.
*/
dest = rib_dest_from_rnode(rn);
RNODE_FOREACH_RE_SAFE (rn, re, same) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
/* If the route was used earlier, must retain it. */
if (dest && re == dest->selected_fib)
continue;
if (IS_ZEBRA_DEBUG_RIB)
rnode_debug(rn, re->vrf_id,
"rn %p, removing unneeded re %p",
rn, re);
rib_unlink(rn, re);
}
}
route_unlock_node(rn);
if (ere->re_nhe)
zebra_nhg_free(ere->re_nhe);
XFREE(MTYPE_WQ_WRAPPER, ere);
}
static void process_subq_early_route_delete(struct zebra_early_route *ere)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *re;
struct route_entry *fib = NULL;
struct route_entry *same = NULL;
struct nexthop *rtnh;
char buf2[INET6_ADDRSTRLEN];
rib_dest_t *dest;
if (ere->src_p_provided)
assert(!ere->src_p.prefixlen || ere->afi == AFI_IP6);
/* Lookup table. */
table = zebra_vrf_lookup_table_with_table_id(
ere->afi, ere->safi, ere->re->vrf_id, ere->re->table);
if (!table) {
early_route_memory_free(ere);
return;
}
/* Apply mask. */
apply_mask(&ere->p);
if (ere->src_p_provided)
apply_mask_ipv6(&ere->src_p);
/* Lookup route node. */
rn = srcdest_rnode_lookup(table, &ere->p,
ere->src_p_provided ? &ere->src_p : NULL);
if (!rn) {
if (IS_ZEBRA_DEBUG_RIB) {
char src_buf[PREFIX_STRLEN];
struct vrf *vrf = vrf_lookup_by_id(ere->re->vrf_id);
if (ere->src_p_provided && ere->src_p.prefixlen)
prefix2str(&ere->src_p, src_buf,
sizeof(src_buf));
else
src_buf[0] = '\0';
zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
vrf->name, ere->re->table, rn,
(src_buf[0] != '\0') ? " from " : "",
src_buf);
}
early_route_memory_free(ere);
return;
}
dest = rib_dest_from_rnode(rn);
fib = dest->selected_fib;
struct nexthop *nh = NULL;
if (ere->re->nhe)
nh = ere->re->nhe->nhg.nexthop;
/* Lookup same type route. */
RNODE_FOREACH_RE (rn, re) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
if (re->type != ere->re->type)
continue;
if (re->instance != ere->re->instance)
continue;
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
ere->re->distance != re->distance)
continue;
if (re->type == ZEBRA_ROUTE_KERNEL &&
re->metric != ere->re->metric)
continue;
if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = nh) &&
rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
if (rtnh->ifindex != nh->ifindex)
continue;
same = re;
break;
}
/* Make sure that the route found has the same gateway. */
if (ere->re->nhe_id && re->nhe_id == ere->re->nhe_id) {
same = re;
break;
}
if (nh == NULL) {
same = re;
break;
}
for (ALL_NEXTHOPS(re->nhe->nhg, rtnh)) {
/*
* No guarantee all kernel send nh with labels
* on delete.
*/
if (nexthop_same_no_labels(rtnh, nh)) {
same = re;
break;
}
}
if (same)
break;
}
/*
* If same type of route can't be found and this message is from
* kernel.
*/
if (!same) {
/*
* In the past(HA!) we could get here because
* we were receiving a route delete from the
* kernel and we're not marking the proto
* as coming from it's appropriate originator.
* Now that we are properly noticing the fact
* that the kernel has deleted our route we
* are not going to get called in this path
* I am going to leave this here because
* this might still work this way on non-linux
* platforms as well as some weird state I have
* not properly thought of yet.
* If we can show that this code path is
* dead then we can remove it.
*/
if (fib && CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE)) {
if (IS_ZEBRA_DEBUG_RIB) {
rnode_debug(
rn, ere->re->vrf_id,
"rn %p, re %p (%s) was deleted from kernel, adding",
rn, fib, zebra_route_string(fib->type));
}
if (zrouter.allow_delete ||
CHECK_FLAG(dest->flags, RIB_ROUTE_ANY_QUEUED)) {
UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
/* Unset flags. */
for (rtnh = fib->nhe->nhg.nexthop; rtnh;
rtnh = rtnh->next)
UNSET_FLAG(rtnh->flags,
NEXTHOP_FLAG_FIB);
/*
* This is a non FRR route
* as such we should mark
* it as deleted
*/
dest->selected_fib = NULL;
} else {
/*
* This means someone else, other than Zebra,
* has deleted a Zebra router from the kernel.
* We will add it back
*/
rib_install_kernel(rn, fib, NULL);
}
} else {
if (IS_ZEBRA_DEBUG_RIB) {
if (nh)
rnode_debug(
rn, ere->re->vrf_id,
"via %s ifindex %d type %d doesn't exist in rib",
inet_ntop(afi2family(ere->afi),
&nh->gate, buf2,
sizeof(buf2)),
nh->ifindex, ere->re->type);
else
rnode_debug(
rn, ere->re->vrf_id,
"type %d doesn't exist in rib",
ere->re->type);
}
route_unlock_node(rn);
early_route_memory_free(ere);
return;
}
}
if (same) {
struct nexthop *tmp_nh;
if (ere->fromkernel &&
CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE) &&
!zrouter.allow_delete) {
rib_install_kernel(rn, same, NULL);
route_unlock_node(rn);
early_route_memory_free(ere);
return;
}
/* Special handling for IPv4 or IPv6 routes sourced from
* EVPN - the nexthop (and associated MAC) need to be
* uninstalled if no more refs.
*/
for (ALL_NEXTHOPS(re->nhe->nhg, tmp_nh)) {
struct ipaddr vtep_ip;
if (CHECK_FLAG(tmp_nh->flags, NEXTHOP_FLAG_EVPN)) {
memset(&vtep_ip, 0, sizeof(struct ipaddr));
if (ere->afi == AFI_IP) {
vtep_ip.ipa_type = IPADDR_V4;
memcpy(&(vtep_ip.ipaddr_v4),
&(tmp_nh->gate.ipv4),
sizeof(struct in_addr));
} else {
vtep_ip.ipa_type = IPADDR_V6;
memcpy(&(vtep_ip.ipaddr_v6),
&(tmp_nh->gate.ipv6),
sizeof(struct in6_addr));
}
zebra_rib_queue_evpn_route_del(
re->vrf_id, &vtep_ip, &ere->p);
}
}
/* Notify dplane if system route changes */
if (RIB_SYSTEM_ROUTE(re))
dplane_sys_route_del(rn, same);
rib_delnode(rn, same);
}
route_unlock_node(rn);
early_route_memory_free(ere);
}
/*
* When FRR receives a route we need to match the route up to
* nexthop groups. That we also may have just received
* place the data on this queue so that this work of finding
* the nexthop group entries for the route entry is always
* done after the nexthop group has had a chance to be processed
*/
static void process_subq_early_route(struct listnode *lnode)
{
struct zebra_early_route *ere = listgetdata(lnode);
if (ere->deletion)
process_subq_early_route_delete(ere);
else
process_subq_early_route_add(ere);
}
/*
* Examine the specified subqueue; process one entry and return 1 if
* there is a node, return 0 otherwise.
*/
static unsigned int process_subq(struct list *subq,
enum meta_queue_indexes qindex)
{
struct listnode *lnode = listhead(subq);
if (!lnode)
return 0;
switch (qindex) {
case META_QUEUE_EVPN:
process_subq_evpn(lnode);
break;
case META_QUEUE_NHG:
process_subq_nhg(lnode);
break;
case META_QUEUE_EARLY_ROUTE:
process_subq_early_route(lnode);
break;
case META_QUEUE_EARLY_LABEL:
process_subq_early_label(lnode);
break;
case META_QUEUE_CONNECTED:
case META_QUEUE_KERNEL:
case META_QUEUE_STATIC:
case META_QUEUE_NOTBGP:
case META_QUEUE_BGP:
case META_QUEUE_OTHER:
process_subq_route(lnode, qindex);
break;
}
list_delete_node(subq, lnode);
return 1;
}
/* Dispatch the meta queue by picking and processing the next node from
* a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
* data is pointed to the meta queue structure.
*/
static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
{
struct meta_queue *mq = data;
unsigned i;
uint32_t queue_len, queue_limit;
/* Ensure there's room for more dataplane updates */
queue_limit = dplane_get_in_queue_limit();
queue_len = dplane_get_in_queue_len();
if (queue_len > queue_limit) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"rib queue: dplane queue len %u, limit %u, retrying",
queue_len, queue_limit);
/* Ensure that the meta-queue is actually enqueued */
if (work_queue_empty(zrouter.ribq))
work_queue_add(zrouter.ribq, zrouter.mq);
return WQ_QUEUE_BLOCKED;
}
for (i = 0; i < MQ_SIZE; i++)
if (process_subq(mq->subq[i], i)) {
mq->size--;
break;
}
return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
}
/*
* Look into the RN and queue it into the highest priority queue
* at this point in time for processing.
*
* We will enqueue a route node only once per invocation.
*
* There are two possibilities here that should be kept in mind.
* If the original invocation has not been pulled off for processing
* yet, A subsuquent invocation can have a route entry with a better
* meta queue index value and we can have a situation where
* we might have the same node enqueued 2 times. Not necessarily
* an optimal situation but it should be ok.
*
* The other possibility is that the original invocation has not
* been pulled off for processing yet, A subsusquent invocation
* doesn't have a route_entry with a better meta-queue and the
* original metaqueue index value will win and we'll end up with
* the route node enqueued once.
*/
static int rib_meta_queue_add(struct meta_queue *mq, void *data)
{
struct route_node *rn = NULL;
struct route_entry *re = NULL, *curr_re = NULL;
uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
rn = (struct route_node *)data;
RNODE_FOREACH_RE (rn, curr_re) {
curr_qindex = route_info[curr_re->type].meta_q_map;
if (curr_qindex <= qindex) {
re = curr_re;
qindex = curr_qindex;
}
}
if (!re)
return -1;
/* Invariant: at this point we always have rn->info set. */
if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
RIB_ROUTE_QUEUED(qindex))) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
rnode_debug(rn, re->vrf_id,
"rn %p is already queued in sub-queue %s",
(void *)rn, subqueue2str(qindex));
return -1;
}
SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
listnode_add(mq->subq[qindex], rn);
route_lock_node(rn);
mq->size++;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %s",
(void *)rn, subqueue2str(qindex));
return 0;
}
static int early_label_meta_queue_add(struct meta_queue *mq, void *data)
{
listnode_add(mq->subq[META_QUEUE_EARLY_LABEL], data);
mq->size++;
return 0;
}
static int rib_meta_queue_nhg_ctx_add(struct meta_queue *mq, void *data)
{
struct nhg_ctx *ctx = NULL;
uint8_t qindex = META_QUEUE_NHG;
struct wq_nhg_wrapper *w;
ctx = (struct nhg_ctx *)data;
if (!ctx)
return -1;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
w->type = WQ_NHG_WRAPPER_TYPE_CTX;
w->u.ctx = ctx;
listnode_add(mq->subq[qindex], w);
mq->size++;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("NHG Context id=%u queued into sub-queue %s",
ctx->id, subqueue2str(qindex));
return 0;
}
static int rib_meta_queue_nhg_add(struct meta_queue *mq, void *data)
{
struct nhg_hash_entry *nhe = NULL;
uint8_t qindex = META_QUEUE_NHG;
struct wq_nhg_wrapper *w;
nhe = (struct nhg_hash_entry *)data;
if (!nhe)
return -1;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
w->type = WQ_NHG_WRAPPER_TYPE_NHG;
w->u.nhe = nhe;
listnode_add(mq->subq[qindex], w);
mq->size++;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("NHG id=%u queued into sub-queue %s", nhe->id,
subqueue2str(qindex));
return 0;
}
static int rib_meta_queue_evpn_add(struct meta_queue *mq, void *data)
{
listnode_add(mq->subq[META_QUEUE_EVPN], data);
mq->size++;
return 0;
}
static int mq_add_handler(void *data,
int (*mq_add_func)(struct meta_queue *mq, void *data))
{
if (zrouter.ribq == NULL) {
flog_err(EC_ZEBRA_WQ_NONEXISTENT,
"%s: work_queue does not exist!", __func__);
return -1;
}
/*
* The RIB queue should normally be either empty or holding the only
* work_queue_item element. In the latter case this element would
* hold a pointer to the meta queue structure, which must be used to
* actually queue the route nodes to process. So create the MQ
* holder, if necessary, then push the work into it in any case.
* This semantics was introduced after 0.99.9 release.
*/
if (work_queue_empty(zrouter.ribq))
work_queue_add(zrouter.ribq, zrouter.mq);
return mq_add_func(zrouter.mq, data);
}
void mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
struct prefix *prefix, uint8_t route_type,
uint8_t route_instance)
{
struct wq_label_wrapper *w;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
w->type = WQ_LABEL_FTN_UNINSTALL;
w->vrf_id = zvrf->vrf->vrf_id;
w->p = *prefix;
w->ltype = type;
w->route_type = route_type;
w->route_instance = route_instance;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("Early Label Handling for %pFX", prefix);
mq_add_handler(w, early_label_meta_queue_add);
}
void mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
const struct zapi_labels *zl)
{
struct wq_label_wrapper *w;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
w->type = WQ_LABEL_LABELS_PROCESS;
w->vrf_id = zvrf->vrf->vrf_id;
w->add_p = add_p;
w->zl = *zl;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("Early Label Handling: Labels Process");
mq_add_handler(w, early_label_meta_queue_add);
}
/* Add route_node to work queue and schedule processing */
int rib_queue_add(struct route_node *rn)
{
assert(rn);
/* Pointless to queue a route_node with no RIB entries to add or remove
*/
if (!rnode_to_ribs(rn)) {
zlog_debug("%s: called for route_node (%p, %u) with no ribs",
__func__, (void *)rn, route_node_get_lock_count(rn));
zlog_backtrace(LOG_DEBUG);
return -1;
}
return mq_add_handler(rn, rib_meta_queue_add);
}
/*
* Enqueue incoming nhg info from OS for processing
*/
int rib_queue_nhg_ctx_add(struct nhg_ctx *ctx)
{
assert(ctx);
return mq_add_handler(ctx, rib_meta_queue_nhg_ctx_add);
}
/*
* Enqueue incoming nhg from proto daemon for processing
*/
int rib_queue_nhe_add(struct nhg_hash_entry *nhe)
{
if (nhe == NULL)
return -1;
return mq_add_handler(nhe, rib_meta_queue_nhg_add);
}
/*
* Enqueue evpn route for processing
*/
int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac,
const struct ipaddr *vtep_ip,
const struct prefix *host_prefix)
{
struct wq_evpn_wrapper *w;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
w->add_p = true;
w->vrf_id = vrf_id;
w->macaddr = *rmac;
w->ip = *vtep_ip;
w->prefix = *host_prefix;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
vrf_id, vtep_ip, host_prefix);
return mq_add_handler(w, rib_meta_queue_evpn_add);
}
int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id,
const struct ipaddr *vtep_ip,
const struct prefix *host_prefix)
{
struct wq_evpn_wrapper *w;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
w->add_p = false;
w->vrf_id = vrf_id;
w->ip = *vtep_ip;
w->prefix = *host_prefix;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
vrf_id, vtep_ip, host_prefix);
return mq_add_handler(w, rib_meta_queue_evpn_add);
}
/* Enqueue EVPN remote ES for processing */
int zebra_rib_queue_evpn_rem_es_add(const esi_t *esi,
const struct in_addr *vtep_ip,
bool esr_rxed, uint8_t df_alg,
uint16_t df_pref)
{
struct wq_evpn_wrapper *w;
char buf[ESI_STR_LEN];
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
w->add_p = true;
w->esi = *esi;
w->ip.ipa_type = IPADDR_V4;
w->ip.ipaddr_v4 = *vtep_ip;
w->esr_rxed = esr_rxed;
w->df_alg = df_alg;
w->df_pref = df_pref;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
esi_to_str(esi, buf, sizeof(buf)));
return mq_add_handler(w, rib_meta_queue_evpn_add);
}
int zebra_rib_queue_evpn_rem_es_del(const esi_t *esi,
const struct in_addr *vtep_ip)
{
struct wq_evpn_wrapper *w;
char buf[ESI_STR_LEN];
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
w->add_p = false;
w->esi = *esi;
w->ip.ipa_type = IPADDR_V4;
w->ip.ipaddr_v4 = *vtep_ip;
if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
esi_to_str(esi, buf, sizeof(buf));
else
strlcpy(buf, "-", sizeof(buf));
zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
buf);
}
return mq_add_handler(w, rib_meta_queue_evpn_add);
}
/*
* Enqueue EVPN remote macip update for processing
*/
int zebra_rib_queue_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr,
const struct ipaddr *ipaddr,
uint8_t flags, uint32_t seq,
struct in_addr vtep_ip, const esi_t *esi)
{
struct wq_evpn_wrapper *w;
char buf[ESI_STR_LEN];
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
w->add_p = true;
w->vni = vni;
w->macaddr = *macaddr;
w->ip = *ipaddr;
w->flags = flags;
w->seq = seq;
w->vtep_ip = vtep_ip;
w->esi = *esi;
if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
esi_to_str(esi, buf, sizeof(buf));
else
strlcpy(buf, "-", sizeof(buf));
zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__,
macaddr, &vtep_ip, buf);
}
return mq_add_handler(w, rib_meta_queue_evpn_add);
}
int zebra_rib_queue_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr,
const struct ipaddr *ip,
struct in_addr vtep_ip)
{
struct wq_evpn_wrapper *w;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
w->add_p = false;
w->vni = vni;
w->macaddr = *macaddr;
w->ip = *ip;
w->vtep_ip = vtep_ip;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__,
macaddr, &vtep_ip);
return mq_add_handler(w, rib_meta_queue_evpn_add);
}
/*
* Enqueue remote VTEP address for processing
*/
int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id, vni_t vni,
struct in_addr vtep_ip, int flood_control)
{
struct wq_evpn_wrapper *w;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
w->add_p = true;
w->vrf_id = vrf_id;
w->vni = vni;
w->vtep_ip = vtep_ip;
w->flags = flood_control;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
&vtep_ip);
return mq_add_handler(w, rib_meta_queue_evpn_add);
}
int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id, vni_t vni,
struct in_addr vtep_ip)
{
struct wq_evpn_wrapper *w;
w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
w->add_p = false;
w->vrf_id = vrf_id;
w->vni = vni;
w->vtep_ip = vtep_ip;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
&vtep_ip);
return mq_add_handler(w, rib_meta_queue_evpn_add);
}
/* Create new meta queue.
A destructor function doesn't seem to be necessary here.
*/
static struct meta_queue *meta_queue_new(void)
{
struct meta_queue *new;
unsigned i;
new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
for (i = 0; i < MQ_SIZE; i++) {
new->subq[i] = list_new();
assert(new->subq[i]);
}
return new;
}
/* Clean up the EVPN meta-queue list */
static void evpn_meta_queue_free(struct meta_queue *mq, struct list *l,
struct zebra_vrf *zvrf)
{
struct listnode *node, *nnode;
struct wq_evpn_wrapper *w;
/* Free the node wrapper object, and the struct it wraps */
for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
if (zvrf) {
vrf_id_t vrf_id = zvrf->vrf->vrf_id;
if (w->vrf_id != vrf_id)
continue;
}
node->data = NULL;
XFREE(MTYPE_WQ_WRAPPER, w);
list_delete_node(l, node);
mq->size--;
}
}
/* Clean up the nhg meta-queue list */
static void nhg_meta_queue_free(struct meta_queue *mq, struct list *l,
struct zebra_vrf *zvrf)
{
struct wq_nhg_wrapper *w;
struct listnode *node, *nnode;
/* Free the node wrapper object, and the struct it wraps */
for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
if (zvrf) {
vrf_id_t vrf_id = zvrf->vrf->vrf_id;
if (w->type == WQ_NHG_WRAPPER_TYPE_CTX &&
w->u.ctx->vrf_id != vrf_id)
continue;
else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG &&
w->u.nhe->vrf_id != vrf_id)
continue;
}
if (w->type == WQ_NHG_WRAPPER_TYPE_CTX)
nhg_ctx_free(&w->u.ctx);
else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG)
zebra_nhg_free(w->u.nhe);
node->data = NULL;
XFREE(MTYPE_WQ_WRAPPER, w);
list_delete_node(l, node);
mq->size--;
}
}
static void early_label_meta_queue_free(struct meta_queue *mq, struct list *l,
struct zebra_vrf *zvrf)
{
struct wq_label_wrapper *w;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
if (zvrf && zvrf->vrf->vrf_id != w->vrf_id)
continue;
switch (w->type) {
case WQ_LABEL_FTN_UNINSTALL:
case WQ_LABEL_LABELS_PROCESS:
break;
}
node->data = NULL;
XFREE(MTYPE_WQ_WRAPPER, w);
list_delete_node(l, node);
mq->size--;
}
}
static void rib_meta_queue_free(struct meta_queue *mq, struct list *l,
struct zebra_vrf *zvrf)
{
struct route_node *rnode;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(l, node, nnode, rnode)) {
rib_dest_t *dest = rib_dest_from_rnode(rnode);
if (dest && rib_dest_vrf(dest) != zvrf)
continue;
route_unlock_node(rnode);
node->data = NULL;
list_delete_node(l, node);
mq->size--;
}
}
static void early_route_meta_queue_free(struct meta_queue *mq, struct list *l,
struct zebra_vrf *zvrf)
{
struct zebra_early_route *zer;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(l, node, nnode, zer)) {
if (zvrf && zer->re->vrf_id != zvrf->vrf->vrf_id)
continue;
XFREE(MTYPE_RE, zer);
node->data = NULL;
list_delete_node(l, node);
mq->size--;
}
}
void meta_queue_free(struct meta_queue *mq, struct zebra_vrf *zvrf)
{
enum meta_queue_indexes i;
for (i = 0; i < MQ_SIZE; i++) {
/* Some subqueues may need cleanup - nhgs for example */
switch (i) {
case META_QUEUE_NHG:
nhg_meta_queue_free(mq, mq->subq[i], zvrf);
break;
case META_QUEUE_EVPN:
evpn_meta_queue_free(mq, mq->subq[i], zvrf);
break;
case META_QUEUE_EARLY_ROUTE:
early_route_meta_queue_free(mq, mq->subq[i], zvrf);
break;
case META_QUEUE_EARLY_LABEL:
early_label_meta_queue_free(mq, mq->subq[i], zvrf);
break;
case META_QUEUE_CONNECTED:
case META_QUEUE_KERNEL:
case META_QUEUE_STATIC:
case META_QUEUE_NOTBGP:
case META_QUEUE_BGP:
case META_QUEUE_OTHER:
rib_meta_queue_free(mq, mq->subq[i], zvrf);
break;
}
if (!zvrf)
list_delete(&mq->subq[i]);
}
if (!zvrf)
XFREE(MTYPE_WORK_QUEUE, mq);
}
/* initialise zebra rib work queue */
static void rib_queue_init(void)
{
if (!(zrouter.ribq = work_queue_new(zrouter.master,
"route_node processing"))) {
flog_err(EC_ZEBRA_WQ_NONEXISTENT,
"%s: could not initialise work queue!", __func__);
return;
}
/* fill in the work queue spec */
zrouter.ribq->spec.workfunc = &meta_queue_process;
zrouter.ribq->spec.errorfunc = NULL;
zrouter.ribq->spec.completion_func = NULL;
/* XXX: TODO: These should be runtime configurable via vty */
zrouter.ribq->spec.max_retries = 3;
zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
if (!(zrouter.mq = meta_queue_new())) {
flog_err(EC_ZEBRA_WQ_NONEXISTENT,
"%s: could not initialise meta queue!", __func__);
return;
}
return;
}
rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
{
rib_dest_t *dest;
dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
rnh_list_init(&dest->nht);
re_list_init(&dest->routes);
route_lock_node(rn); /* rn route table reference */
rn->info = dest;
dest->rnode = rn;
return dest;
}
/* RIB updates are processed via a queue of pointers to route_nodes.
*
* The queue length is bounded by the maximal size of the routing table,
* as a route_node will not be requeued, if already queued.
*
* REs are submitted via rib_addnode or rib_delnode which set minimal
* state, or static_install_route (when an existing RE is updated)
* and then submit route_node to queue for best-path selection later.
* Order of add/delete state changes are preserved for any given RE.
*
* Deleted REs are reaped during best-path selection.
*
* rib_addnode
* |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
* |-------->| | best RE, if required
* | |
* static_install->|->rib_addqueue...... -> rib_process
* | |
* |-------->| |-> rib_unlink
* |-> set ROUTE_ENTRY_REMOVE |
* rib_delnode (RE freed)
*
* The 'info' pointer of a route_node points to a rib_dest_t
* ('dest'). Queueing state for a route_node is kept on the dest. The
* dest is created on-demand by rib_link() and is kept around at least
* as long as there are ribs hanging off it (@see rib_gc_dest()).
*
* Refcounting (aka "locking" throughout the Zebra and FRR code):
*
* - route_nodes: refcounted by:
* - dest attached to route_node:
* - managed by: rib_link/rib_gc_dest
* - route_node processing queue
* - managed by: rib_addqueue, rib_process.
*
*/
/* Add RE to head of the route node. */
static void rib_link(struct route_node *rn, struct route_entry *re, int process)
{
rib_dest_t *dest;
afi_t afi;
const char *rmap_name;
assert(re && rn);
dest = rib_dest_from_rnode(rn);
if (!dest) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
dest = zebra_rib_create_dest(rn);
}
re_list_add_head(&dest->routes, re);
afi = (rn->p.family == AF_INET)
? AFI_IP
: (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
rmap_name = zebra_get_import_table_route_map(afi, re->table);
zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
}
if (process)
rib_queue_add(rn);
}
static void rib_addnode(struct route_node *rn,
struct route_entry *re, int process)
{
/* RE node has been un-removed before route-node is processed.
* route_node must hence already be on the queue for processing..
*/
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
if (IS_ZEBRA_DEBUG_RIB)
rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
(void *)rn, (void *)re);
UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
return;
}
rib_link(rn, re, process);
}
/*
* rib_unlink
*
* Detach a rib structure from a route_node.
*
* Note that a call to rib_unlink() should be followed by a call to
* rib_gc_dest() at some point. This allows a rib_dest_t that is no
* longer required to be deleted.
*/
void rib_unlink(struct route_node *rn, struct route_entry *re)
{
rib_dest_t *dest;
assert(rn && re);
if (IS_ZEBRA_DEBUG_RIB)
rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
(void *)re);
dest = rib_dest_from_rnode(rn);
re_list_del(&dest->routes, re);
if (dest->selected_fib == re)
dest->selected_fib = NULL;
rib_re_nhg_free(re);
zapi_re_opaque_free(re->opaque);
XFREE(MTYPE_RE, re);
}
void rib_delnode(struct route_node *rn, struct route_entry *re)
{
afi_t afi;
if (IS_ZEBRA_DEBUG_RIB)
rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
(void *)rn, (void *)re);
SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
afi = (rn->p.family == AF_INET)
? AFI_IP
: (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
zebra_del_import_table_entry(zvrf, rn, re);
/* Just clean up if non main table */
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
vrf_id_to_name(re->vrf_id), re->vrf_id, rn,
rn, re, zebra_route_string(re->type));
}
rib_queue_add(rn);
}
/*
* Helper that debugs a single nexthop within a route-entry
*/
static void _route_entry_dump_nh(const struct route_entry *re,
const char *straddr,
const struct nexthop *nexthop)
{
char nhname[PREFIX_STRLEN];
char backup_str[50];
char wgt_str[50];
char temp_str[10];
char label_str[MPLS_LABEL_STRLEN];
int i;
struct interface *ifp;
struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
switch (nexthop->type) {
case NEXTHOP_TYPE_BLACKHOLE:
snprintf(nhname, sizeof(nhname), "Blackhole");
break;
case NEXTHOP_TYPE_IFINDEX:
ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
snprintf(nhname, sizeof(nhname), "%s",
ifp ? ifp->name : "Unknown");
break;
case NEXTHOP_TYPE_IPV4:
/* fallthrough */
case NEXTHOP_TYPE_IPV4_IFINDEX:
inet_ntop(AF_INET, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
inet_ntop(AF_INET6, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
break;
}
/* Label stack */
label_str[0] = '\0';
if (nexthop->nh_label && nexthop->nh_label->num_labels > 0) {
mpls_label2str(nexthop->nh_label->num_labels,
nexthop->nh_label->label, label_str,
sizeof(label_str), 0 /*pretty*/);
strlcat(label_str, ", ", sizeof(label_str));
}
backup_str[0] = '\0';
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
snprintf(backup_str, sizeof(backup_str), "backup ");
for (i = 0; i < nexthop->backup_num; i++) {
snprintf(temp_str, sizeof(temp_str), "%d, ",
nexthop->backup_idx[i]);
strlcat(backup_str, temp_str, sizeof(backup_str));
}
}
wgt_str[0] = '\0';
if (nexthop->weight)
snprintf(wgt_str, sizeof(wgt_str), "wgt %d,", nexthop->weight);
zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
straddr, (nexthop->rparent ? " NH" : "NH"), nhname,
nexthop->ifindex, label_str, vrf ? vrf->name : "Unknown",
nexthop->vrf_id,
wgt_str, backup_str,
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
? "ACTIVE "
: ""),
(CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
? "FIB "
: ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
? "RECURSIVE "
: ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
? "ONLINK "
: ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
? "DUPLICATE "
: ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED)
? "FILTERED " : ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)
? "BACKUP " : ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE)
? "SRTE " : ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN)
? "EVPN " : ""));
}
/* This function dumps the contents of a given RE entry into
* standard debug log. Calling function name and IP prefix in
* question are passed as 1st and 2nd arguments.
*/
void _route_entry_dump(const char *func, union prefixconstptr pp,
union prefixconstptr src_pp,
const struct route_entry *re)
{
const struct prefix *src_p = src_pp.p;
bool is_srcdst = src_p && src_p->prefixlen;
char straddr[PREFIX_STRLEN];
char srcaddr[PREFIX_STRLEN];
char flags_buf[128];
char status_buf[128];
struct nexthop *nexthop;
struct vrf *vrf = vrf_lookup_by_id(re->vrf_id);
struct nexthop_group *nhg;
prefix2str(pp, straddr, sizeof(straddr));
zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func,
(const void *)re, straddr,
is_srcdst ? " from " : "",
is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
: "",
VRF_LOGNAME(vrf), re->vrf_id);
zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
straddr, (unsigned long)re->uptime, re->type, re->instance,
re->table);
zlog_debug(
"%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
straddr, re->metric, re->mtu, re->distance,
zclient_dump_route_flags(re->flags, flags_buf,
sizeof(flags_buf)),
_dump_re_status(re, status_buf, sizeof(status_buf)));
zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr,
nexthop_group_nexthop_num(&(re->nhe->nhg)),
nexthop_group_active_nexthop_num(&(re->nhe->nhg)));
/* Dump nexthops */
for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
_route_entry_dump_nh(re, straddr, nexthop);
if (zebra_nhg_get_backup_nhg(re->nhe)) {
zlog_debug("%s: backup nexthops:", straddr);
nhg = zebra_nhg_get_backup_nhg(re->nhe);
for (ALL_NEXTHOPS_PTR(nhg, nexthop))
_route_entry_dump_nh(re, straddr, nexthop);
}
zlog_debug("%s: dump complete", straddr);
}
static int rib_meta_queue_early_route_add(struct meta_queue *mq, void *data)
{
struct zebra_early_route *ere = data;
listnode_add(mq->subq[META_QUEUE_EARLY_ROUTE], data);
mq->size++;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"Route %pFX(%u) queued for processing into sub-queue %s",
&ere->p, ere->re->vrf_id,
subqueue2str(META_QUEUE_EARLY_ROUTE));
return 0;
}
struct route_entry *zebra_rib_route_entry_new(vrf_id_t vrf_id, int type,
uint8_t instance, uint32_t flags,
uint32_t nhe_id,
uint32_t table_id,
uint32_t metric, uint32_t mtu,
uint8_t distance, route_tag_t tag)
{
struct route_entry *re;
re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
re->type = type;
re->instance = instance;
re->distance = distance;
re->flags = flags;
re->metric = metric;
re->mtu = mtu;
re->table = table_id;
re->vrf_id = vrf_id;
re->uptime = monotime(NULL);
re->tag = tag;
re->nhe_id = nhe_id;
return re;
}
/*
* Internal route-add implementation; there are a couple of different public
* signatures. Callers in this path are responsible for the memory they
* allocate: if they allocate a nexthop_group or backup nexthop info, they
* must free those objects. If this returns < 0, an error has occurred and the
* route_entry 're' has not been captured; the caller should free that also.
*
* -1 -> error
* 0 -> Add
* 1 -> update
*/
int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p,
struct prefix_ipv6 *src_p, struct route_entry *re,
struct nhg_hash_entry *re_nhe, bool startup)
{
struct zebra_early_route *ere;
if (!re)
return -1;
assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
ere->afi = afi;
ere->safi = safi;
ere->p = *p;
if (src_p)
ere->src_p = *src_p;
ere->src_p_provided = !!src_p;
ere->re = re;
ere->re_nhe = re_nhe;
ere->startup = startup;
return mq_add_handler(ere, rib_meta_queue_early_route_add);
}
/*
* Add a single route.
*/
int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
struct prefix_ipv6 *src_p, struct route_entry *re,
struct nexthop_group *ng, bool startup)
{
int ret;
struct nhg_hash_entry nhe, *n;
if (!re)
return -1;
/* We either need nexthop(s) or an existing nexthop id */
if (ng == NULL && re->nhe_id == 0)
return -1;
/*
* Use a temporary nhe to convey info to the common/main api.
*/
zebra_nhe_init(&nhe, afi, (ng ? ng->nexthop : NULL));
if (ng)
nhe.nhg.nexthop = ng->nexthop;
else if (re->nhe_id > 0)
nhe.id = re->nhe_id;
n = zebra_nhe_copy(&nhe, 0);
ret = rib_add_multipath_nhe(afi, safi, p, src_p, re, n, startup);
/* In error cases, free the route also */
if (ret < 0)
XFREE(MTYPE_RE, re);
return ret;
}
void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
unsigned short instance, uint32_t flags, struct prefix *p,
struct prefix_ipv6 *src_p, const struct nexthop *nh,
uint32_t nhe_id, uint32_t table_id, uint32_t metric,
uint8_t distance, bool fromkernel)
{
struct zebra_early_route *ere;
struct route_entry *re = NULL;
struct nhg_hash_entry *nhe = NULL;
re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
table_id, metric, 0, distance, 0);
if (nh) {
nhe = zebra_nhg_alloc();
nhe->nhg.nexthop = nexthop_dup(nh, NULL);
}
ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
ere->afi = afi;
ere->safi = safi;
ere->p = *p;
if (src_p)
ere->src_p = *src_p;
ere->src_p_provided = !!src_p;
ere->re = re;
ere->re_nhe = nhe;
ere->startup = false;
ere->deletion = true;
ere->fromkernel = fromkernel;
mq_add_handler(ere, rib_meta_queue_early_route_add);
}
int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
unsigned short instance, uint32_t flags, struct prefix *p,
struct prefix_ipv6 *src_p, const struct nexthop *nh,
uint32_t nhe_id, uint32_t table_id, uint32_t metric, uint32_t mtu,
uint8_t distance, route_tag_t tag, bool startup)
{
struct route_entry *re = NULL;
struct nexthop nexthop = {};
struct nexthop_group ng = {};
/* Allocate new route_entry structure. */
re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
table_id, metric, mtu, distance, tag);
/* If the owner of the route supplies a shared nexthop-group id,
* we'll use that. Otherwise, pass the nexthop along directly.
*/
if (!nhe_id) {
/* Add nexthop. */
nexthop = *nh;
nexthop_group_add_sorted(&ng, &nexthop);
}
return rib_add_multipath(afi, safi, p, src_p, re, &ng, startup);
}
static const char *rib_update_event2str(enum rib_update_event event)
{
const char *ret = "UNKNOWN";
switch (event) {
case RIB_UPDATE_KERNEL:
ret = "RIB_UPDATE_KERNEL";
break;
case RIB_UPDATE_RMAP_CHANGE:
ret = "RIB_UPDATE_RMAP_CHANGE";
break;
case RIB_UPDATE_OTHER:
ret = "RIB_UPDATE_OTHER";
break;
case RIB_UPDATE_MAX:
break;
}
return ret;
}
/* Schedule route nodes to be processed if they match the type */
static void rib_update_route_node(struct route_node *rn, int type)
{
struct route_entry *re, *next;
bool re_changed = false;
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (type == ZEBRA_ROUTE_ALL || type == re->type) {
SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
re_changed = true;
}
}
if (re_changed)
rib_queue_add(rn);
}
/* Schedule routes of a particular table (address-family) based on event. */
void rib_update_table(struct route_table *table, enum rib_update_event event,
int rtype)
{
struct route_node *rn;
if (IS_ZEBRA_DEBUG_EVENT) {
struct zebra_vrf *zvrf;
struct vrf *vrf;
zvrf = table->info
? ((struct rib_table_info *)table->info)->zvrf
: NULL;
vrf = zvrf ? zvrf->vrf : NULL;
zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__,
table->info ? afi2str(
((struct rib_table_info *)table->info)->afi)
: "Unknown",
VRF_LOGNAME(vrf), zvrf ? zvrf->table_id : 0,
rib_update_event2str(event), zebra_route_string(rtype));
}
/* Walk all routes and queue for processing, if appropriate for
* the trigger event.
*/
for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
/*
* If we are looking at a route node and the node
* has already been queued we don't
* need to queue it up again
*/
if (rn->info
&& CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
RIB_ROUTE_ANY_QUEUED))
continue;
switch (event) {
case RIB_UPDATE_KERNEL:
rib_update_route_node(rn, ZEBRA_ROUTE_KERNEL);
break;
case RIB_UPDATE_RMAP_CHANGE:
case RIB_UPDATE_OTHER:
rib_update_route_node(rn, rtype);
break;
default:
break;
}
}
}
static void rib_update_handle_vrf(vrf_id_t vrf_id, enum rib_update_event event,
int rtype)
{
struct route_table *table;
if (IS_ZEBRA_DEBUG_EVENT)
zlog_debug("%s: Handling VRF %s event %s", __func__,
vrf_id_to_name(vrf_id), rib_update_event2str(event));
/* Process routes of interested address-families. */
table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
if (table)
rib_update_table(table, event, rtype);
table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
if (table)
rib_update_table(table, event, rtype);
}
static void rib_update_handle_vrf_all(enum rib_update_event event, int rtype)
{
struct zebra_router_table *zrt;
if (IS_ZEBRA_DEBUG_EVENT)
zlog_debug("%s: Handling VRF (ALL) event %s", __func__,
rib_update_event2str(event));
/* Just iterate over all the route tables, rather than vrf lookups */
RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables)
rib_update_table(zrt->table, event, rtype);
}
struct rib_update_ctx {
enum rib_update_event event;
bool vrf_all;
vrf_id_t vrf_id;
};
static struct rib_update_ctx *rib_update_ctx_init(vrf_id_t vrf_id,
enum rib_update_event event)
{
struct rib_update_ctx *ctx;
ctx = XCALLOC(MTYPE_RIB_UPDATE_CTX, sizeof(struct rib_update_ctx));
ctx->event = event;
ctx->vrf_id = vrf_id;
return ctx;
}
static void rib_update_ctx_fini(struct rib_update_ctx **ctx)
{
XFREE(MTYPE_RIB_UPDATE_CTX, *ctx);
}
static void rib_update_handler(struct thread *thread)
{
struct rib_update_ctx *ctx;
ctx = THREAD_ARG(thread);
if (ctx->vrf_all)
rib_update_handle_vrf_all(ctx->event, ZEBRA_ROUTE_ALL);
else
rib_update_handle_vrf(ctx->vrf_id, ctx->event, ZEBRA_ROUTE_ALL);
rib_update_ctx_fini(&ctx);
}
/*
* Thread list to ensure we don't schedule a ton of events
* if interfaces are flapping for instance.
*/
static struct thread *t_rib_update_threads[RIB_UPDATE_MAX];
void rib_update_finish(void)
{
int i;
for (i = RIB_UPDATE_KERNEL; i < RIB_UPDATE_MAX; i++) {
if (thread_is_scheduled(t_rib_update_threads[i])) {
struct rib_update_ctx *ctx;
ctx = THREAD_ARG(t_rib_update_threads[i]);
rib_update_ctx_fini(&ctx);
THREAD_OFF(t_rib_update_threads[i]);
}
}
}
/* Schedule a RIB update event for all vrfs */
void rib_update(enum rib_update_event event)
{
struct rib_update_ctx *ctx;
if (thread_is_scheduled(t_rib_update_threads[event]))
return;
if (zebra_router_in_shutdown())
return;
ctx = rib_update_ctx_init(0, event);
ctx->vrf_all = true;
thread_add_event(zrouter.master, rib_update_handler, ctx, 0,
&t_rib_update_threads[event]);
if (IS_ZEBRA_DEBUG_EVENT)
zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__,
rib_update_event2str(event));
}
/* Delete self installed routes after zebra is relaunched. */
void rib_sweep_table(struct route_table *table)
{
struct route_node *rn;
struct route_entry *re;
struct route_entry *next;
struct nexthop *nexthop;
if (!table)
return;
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%s: starting", __func__);
for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (IS_ZEBRA_DEBUG_RIB)
route_entry_dump(&rn->p, NULL, re);
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
continue;
/*
* If routes are older than startup_time then
* we know we read them in from the kernel.
* As such we can safely remove them.
*/
if (zrouter.startup_time < re->uptime)
continue;
/*
* So we are starting up and have received
* routes from the kernel that we have installed
* from a previous run of zebra but not cleaned
* up ( say a kill -9 )
* But since we haven't actually installed
* them yet( we received them from the kernel )
* we don't think they are active.
* So let's pretend they are active to actually
* remove them.
* In all honesty I'm not sure if we should
* mark them as active when we receive them
* This is startup only so probably ok.
*
* If we ever decide to move rib_sweep_table
* to a different spot (ie startup )
* this decision needs to be revisited
*/
SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
rib_uninstall_kernel(rn, re);
rib_delnode(rn, re);
}
}
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%s: ends", __func__);
}
/* Sweep all RIB tables. */
void rib_sweep_route(struct thread *t)
{
struct vrf *vrf;
struct zebra_vrf *zvrf;
RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
if ((zvrf = vrf->info) == NULL)
continue;
rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
}
zebra_router_sweep_route();
zebra_router_sweep_nhgs();
}
/* Remove specific by protocol routes from 'table'. */
unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
struct route_table *table)
{
struct route_node *rn;
struct route_entry *re;
struct route_entry *next;
unsigned long n = 0;
if (table)
for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
if (re->type == proto
&& re->instance == instance) {
rib_delnode(rn, re);
n++;
}
}
return n;
}
/* Remove specific by protocol routes. */
unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
{
struct vrf *vrf;
struct zebra_vrf *zvrf;
struct other_route_table *ort;
unsigned long cnt = 0;
RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
zvrf = vrf->info;
if (!zvrf)
continue;
cnt += rib_score_proto_table(proto, instance,
zvrf->table[AFI_IP][SAFI_UNICAST])
+ rib_score_proto_table(
proto, instance,
zvrf->table[AFI_IP6][SAFI_UNICAST]);
frr_each(otable, &zvrf->other_tables, ort) cnt +=
rib_score_proto_table(proto, instance, ort->table);
}
return cnt;
}
/* Close RIB and clean up kernel routes. */
void rib_close_table(struct route_table *table)
{
struct route_node *rn;
rib_dest_t *dest;
if (!table)
return;
for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
dest = rib_dest_from_rnode(rn);
if (dest && dest->selected_fib) {
rib_uninstall_kernel(rn, dest->selected_fib);
dest->selected_fib = NULL;
}
}
}
/*
* Handler for async dataplane results after a pseudowire installation
*/
static void handle_pw_result(struct zebra_dplane_ctx *ctx)
{
struct zebra_pw *pw;
struct zebra_vrf *vrf;
/* The pseudowire code assumes success - we act on an error
* result for installation attempts here.
*/
if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
return;
if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
pw = zebra_pw_find(vrf, dplane_ctx_get_ifname(ctx));
if (pw)
zebra_pw_install_failure(pw,
dplane_ctx_get_pw_status(ctx));
}
}
/*
* Handle results from the dataplane system. Dequeue update context
* structs, dispatch to appropriate internal handlers.
*/
static void rib_process_dplane_results(struct thread *thread)
{
struct zebra_dplane_ctx *ctx;
struct dplane_ctx_q ctxlist;
bool shut_p = false;
#ifdef HAVE_SCRIPTING
char *script_name =
frrscript_names_get_script_name(ZEBRA_ON_RIB_PROCESS_HOOK_CALL);
int ret = 1;
struct frrscript *fs = NULL;
if (script_name) {
fs = frrscript_new(script_name);
if (fs)
ret = frrscript_load(fs, ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
NULL);
}
#endif /* HAVE_SCRIPTING */
/* Dequeue a list of completed updates with one lock/unlock cycle */
do {
TAILQ_INIT(&ctxlist);
/* Take lock controlling queue of results */
frr_with_mutex (&dplane_mutex) {
/* Dequeue list of context structs */
dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
}
/* Dequeue context block */
ctx = dplane_ctx_dequeue(&ctxlist);
/* If we've emptied the results queue, we're done */
if (ctx == NULL)
break;
/* If zebra is shutting down, avoid processing results,
* just drain the results queue.
*/
shut_p = atomic_load_explicit(&zrouter.in_shutdown,
memory_order_relaxed);
if (shut_p) {
while (ctx) {
dplane_ctx_fini(&ctx);
ctx = dplane_ctx_dequeue(&ctxlist);
}
continue;
}
while (ctx) {
#ifdef HAVE_SCRIPTING
if (ret == 0)
frrscript_call(fs,
ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
("ctx", ctx));
#endif /* HAVE_SCRIPTING */
switch (dplane_ctx_get_op(ctx)) {
case DPLANE_OP_ROUTE_INSTALL:
case DPLANE_OP_ROUTE_UPDATE:
case DPLANE_OP_ROUTE_DELETE:
/* Bit of special case for route updates
* that were generated by async notifications:
* we don't want to continue processing these
* in the rib.
*/
if (dplane_ctx_get_notif_provider(ctx) == 0)
rib_process_result(ctx);
break;
case DPLANE_OP_ROUTE_NOTIFY:
rib_process_dplane_notify(ctx);
break;
case DPLANE_OP_NH_INSTALL:
case DPLANE_OP_NH_UPDATE:
case DPLANE_OP_NH_DELETE:
zebra_nhg_dplane_result(ctx);
break;
case DPLANE_OP_LSP_INSTALL:
case DPLANE_OP_LSP_UPDATE:
case DPLANE_OP_LSP_DELETE:
/* Bit of special case for LSP updates
* that were generated by async notifications:
* we don't want to continue processing these.
*/
if (dplane_ctx_get_notif_provider(ctx) == 0)
zebra_mpls_lsp_dplane_result(ctx);
break;
case DPLANE_OP_LSP_NOTIFY:
zebra_mpls_process_dplane_notify(ctx);
break;
case DPLANE_OP_PW_INSTALL:
case DPLANE_OP_PW_UNINSTALL:
handle_pw_result(ctx);
break;
case DPLANE_OP_SYS_ROUTE_ADD:
case DPLANE_OP_SYS_ROUTE_DELETE:
break;
case DPLANE_OP_MAC_INSTALL:
case DPLANE_OP_MAC_DELETE:
zebra_vxlan_handle_result(ctx);
break;
case DPLANE_OP_RULE_ADD:
case DPLANE_OP_RULE_DELETE:
case DPLANE_OP_RULE_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:
zebra_pbr_dplane_result(ctx);
break;
case DPLANE_OP_INTF_ADDR_ADD:
case DPLANE_OP_INTF_ADDR_DEL:
case DPLANE_OP_INTF_INSTALL:
case DPLANE_OP_INTF_UPDATE:
case DPLANE_OP_INTF_DELETE:
case DPLANE_OP_INTF_NETCONFIG:
zebra_if_dplane_result(ctx);
break;
case DPLANE_OP_TC_INSTALL:
case DPLANE_OP_TC_UPDATE:
case DPLANE_OP_TC_DELETE:
break;
/* Some op codes not handled here */
case DPLANE_OP_ADDR_INSTALL:
case DPLANE_OP_ADDR_UNINSTALL:
case DPLANE_OP_NEIGH_INSTALL:
case DPLANE_OP_NEIGH_UPDATE:
case DPLANE_OP_NEIGH_DELETE:
case DPLANE_OP_NEIGH_IP_INSTALL:
case DPLANE_OP_NEIGH_IP_DELETE:
case DPLANE_OP_VTEP_ADD:
case DPLANE_OP_VTEP_DELETE:
case DPLANE_OP_NEIGH_DISCOVER:
case DPLANE_OP_BR_PORT_UPDATE:
case DPLANE_OP_NEIGH_TABLE_UPDATE:
case DPLANE_OP_GRE_SET:
case DPLANE_OP_NONE:
break;
} /* Dispatch by op code */
dplane_ctx_fini(&ctx);
ctx = dplane_ctx_dequeue(&ctxlist);
}
} while (1);
#ifdef HAVE_SCRIPTING
if (fs)
frrscript_delete(fs);
#endif
}
/*
* Results are returned from the dataplane subsystem, in the context of
* the dataplane pthread. We enqueue the results here for processing by
* the main thread later.
*/
static int rib_dplane_results(struct dplane_ctx_q *ctxlist)
{
/* Take lock controlling queue of results */
frr_with_mutex (&dplane_mutex) {
/* Enqueue context blocks */
dplane_ctx_list_append(&rib_dplane_q, ctxlist);
}
/* Ensure event is signalled to zebra main pthread */
thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
&t_dplane);
return 0;
}
/*
* Ensure there are no empty slots in the route_info array.
* Every route type in zebra should be present there.
*/
static void check_route_info(void)
{
int len = array_size(route_info);
/*
* ZEBRA_ROUTE_SYSTEM is special cased since
* its key is 0 anyway.
*
* ZEBRA_ROUTE_ALL is also ignored.
*/
for (int i = 0; i < len; i++) {
assert(route_info[i].key >= ZEBRA_ROUTE_SYSTEM &&
route_info[i].key < ZEBRA_ROUTE_MAX);
assert(route_info[i].meta_q_map < MQ_SIZE);
}
}
/* Routing information base initialize. */
void rib_init(void)
{
check_route_info();
rib_queue_init();
/* Init dataplane, and register for results */
pthread_mutex_init(&dplane_mutex, NULL);
TAILQ_INIT(&rib_dplane_q);
zebra_dplane_init(rib_dplane_results);
}
/*
* vrf_id_get_next
*
* Get the first vrf id that is greater than the given vrf id if any.
*
* Returns true if a vrf id was found, false otherwise.
*/
static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
{
struct vrf *vrf;
vrf = vrf_lookup_by_id(vrf_id);
if (vrf) {
vrf = RB_NEXT(vrf_id_head, vrf);
if (vrf) {
*next_id_p = vrf->vrf_id;
return 1;
}
}
return 0;
}
/*
* rib_tables_iter_next
*
* Returns the next table in the iteration.
*/
struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
{
struct route_table *table;
/*
* Array that helps us go over all AFI/SAFI combinations via one
* index.
*/
static const struct {
afi_t afi;
safi_t safi;
} afi_safis[] = {
{AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
{AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
{AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
};
table = NULL;
switch (iter->state) {
case RIB_TABLES_ITER_S_INIT:
iter->vrf_id = VRF_DEFAULT;
iter->afi_safi_ix = -1;
/* Fall through */
case RIB_TABLES_ITER_S_ITERATING:
iter->afi_safi_ix++;
while (1) {
while (iter->afi_safi_ix
< (int)array_size(afi_safis)) {
table = zebra_vrf_table(
afi_safis[iter->afi_safi_ix].afi,
afi_safis[iter->afi_safi_ix].safi,
iter->vrf_id);
if (table)
break;
iter->afi_safi_ix++;
}
/*
* Found another table in this vrf.
*/
if (table)
break;
/*
* Done with all tables in the current vrf, go to the
* next
* one.
*/
if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
break;
iter->afi_safi_ix = 0;
}
break;
case RIB_TABLES_ITER_S_DONE:
return NULL;
}
if (table)
iter->state = RIB_TABLES_ITER_S_ITERATING;
else
iter->state = RIB_TABLES_ITER_S_DONE;
return table;
}
|