File: binding.c

package info (click to toggle)
ovn 25.09.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,492 kB
  • sloc: ansic: 106,060; xml: 23,314; sh: 3,322; python: 1,838; makefile: 836
file content (3979 lines) | stat: -rw-r--r-- 141,571 bytes parent folder | download
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
/* Copyright (c) 2015, 2016, 2017 Nicira, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

/* OVS includes. */
#include "lib/bitmap.h"
#include "openvswitch/poll-loop.h"
#include "lib/sset.h"
#include "lib/util.h"
#include "lib/netdev.h"
#include "lib/vswitch-idl.h"
#include "openvswitch/hmap.h"
#include "openvswitch/vlog.h"

/* OVN includes. */
#include "binding.h"
#include "ha-chassis.h"
#include "if-status.h"
#include "lflow.h"
#include "lib/chassis-index.h"
#include "lib/ovn-sb-idl.h"
#include "local_data.h"
#include "lport.h"
#include "ovn-controller.h"
#include "patch.h"
#include "ovsport.h"

VLOG_DEFINE_THIS_MODULE(binding);

/* External ID to be set in the OVS.Interface record when the OVS interface
 * is ready for use, i.e., is bound to an OVN port and its corresponding
 * flows have been installed.
 */
#define OVN_INSTALLED_EXT_ID "ovn-installed"
#define OVN_INSTALLED_TS_EXT_ID "ovn-installed-ts"

#define OVN_QOS_TYPE "linux-htb"

#define CLAIM_TIME_THRESHOLD_MS 2000

struct claimed_port {
    long long int last_claimed;
};

struct qos_port {
    bool added;
};

static struct shash _claimed_ports = SHASH_INITIALIZER(&_claimed_ports);
static struct sset _postponed_ports = SSET_INITIALIZER(&_postponed_ports);
static struct shash _qos_ports = SHASH_INITIALIZER(&_qos_ports);

static void
remove_additional_chassis(const struct sbrec_port_binding *pb,
                          const struct sbrec_chassis *chassis_rec);

struct sset *
get_postponed_ports(void)
{
    return &_postponed_ports;
}

static long long int
get_claim_timestamp(const char *port_name)
{
    struct claimed_port *cp = shash_find_data(&_claimed_ports, port_name);
    return cp ? cp->last_claimed : 0;
}

static void
register_claim_timestamp(const char *port_name, long long int t)
{
    struct claimed_port *cp = shash_find_data(&_claimed_ports, port_name);
    if (!cp) {
        cp = xzalloc(sizeof *cp);
        shash_add(&_claimed_ports, port_name, cp);
    }
    cp->last_claimed = t;
}

static void
cleanup_claimed_port_timestamps(void)
{
    long long int now = time_msec();
    struct shash_node *node;
    SHASH_FOR_EACH_SAFE (node, &_claimed_ports) {
        struct claimed_port *cp = (struct claimed_port *) node->data;
        if (now - cp->last_claimed >= 5 * CLAIM_TIME_THRESHOLD_MS) {
            free(cp);
            shash_delete(&_claimed_ports, node);
        }
    }
}

/* Schedule any pending binding work. Runs with in the main ovn-controller
 * thread context.*/
void
binding_wait(void)
{
    const char *port_name;
    SSET_FOR_EACH (port_name, &_postponed_ports) {
        long long int t = get_claim_timestamp(port_name);
        if (t) {
            poll_timer_wait_until(t + CLAIM_TIME_THRESHOLD_MS);
        }
    }
}

void
binding_register_ovs_idl(struct ovsdb_idl *ovs_idl)
{
    ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
    ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);

    ovsdb_idl_add_table(ovs_idl, &ovsrec_table_bridge);
    ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_name);
    ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_ports);

    ovsdb_idl_add_table(ovs_idl, &ovsrec_table_port);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_name);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_interfaces);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_qos);

    ovsdb_idl_add_table(ovs_idl, &ovsrec_table_interface);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_name);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_external_ids);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_bfd);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_bfd_status);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_status);

    ovsdb_idl_add_table(ovs_idl, &ovsrec_table_qos);
    ovsdb_idl_add_column(ovs_idl, &ovsrec_qos_col_type);
}

static void update_lport_tracking(const struct sbrec_port_binding *pb,
                                  struct hmap *tracked_dp_bindings,
                                  bool claimed);

struct qos_queue {
    struct hmap_node node;

    char *network;
    char *port;

    uint32_t queue_id;
    unsigned long long min_rate;
    unsigned long long max_rate;
    unsigned long long burst;
};

static struct qos_queue *
find_qos_queue(struct hmap *queue_map, uint32_t hash, const char *port)
{
    struct qos_queue *q;
    HMAP_FOR_EACH_WITH_HASH (q, node, hash, queue_map) {
        if (!strcmp(q->port, port)) {
            return q;
        }
    }
    return NULL;
}

static void
qos_queue_erase_entry(struct qos_queue *q)
{
    free(q->network);
    free(q->port);
    free(q);
}

void
destroy_qos_map(struct hmap *qos_map)
{
    struct qos_queue *q;
    HMAP_FOR_EACH_POP (q, node, qos_map) {
        qos_queue_erase_entry(q);
    }
    hmap_destroy(qos_map);
}

static bool
is_qos_iface(const struct ovsrec_interface *iface)
{
    if (smap_get_bool(&iface->external_ids, "ovn-egress-iface", false)) {
        return true;
    }

    if (!iface->type[0] || !strcmp(iface->type, "system")) {
        return true;
    }

    return false;
}

static const struct ovsrec_interface *
get_qos_egress_port_interface(struct shash *bridge_mappings,
                              const struct ovsrec_port **pport,
                              const char *network)
{
    struct ovsrec_bridge *br_ln = shash_find_data(bridge_mappings, network);
    if (!br_ln) {
        return NULL;
    }

    /* Add egress-ifaces from the connected bridge */
    for (size_t i = 0; i < br_ln->n_ports; i++) {
        const struct ovsrec_port *port = br_ln->ports[i];
        for (size_t j = 0; j < port->n_interfaces; j++) {
            const struct ovsrec_interface *iface = port->interfaces[j];

            if (smap_get(&iface->external_ids, "iface-id")) {
                continue;
            }

            if (is_qos_iface(iface)) {
                *pport = port;
                return iface;
            }
        }
    }

    return NULL;
}

static void
add_or_del_qos_port(const char *ovn_port, bool add)
{
    struct qos_port *qos_port = shash_find_data(&_qos_ports, ovn_port);
    if (!qos_port) {
        qos_port = xzalloc(sizeof *qos_port);
        shash_add(&_qos_ports, ovn_port, qos_port);
    }
    qos_port->added = add;
}

/* 34359738360 == (2^32 - 1) * 8.  netdev_set_qos() doesn't support
 * 64-bit rate netlink attributes, so the maximum value is 2^32 - 1
 * bytes. The 'max-rate' config option is in bits, so multiplying by 8.
 * Without setting max-rate the reported link speed will be used, which
 * can be unrecognized for certain NICs or reported too low for virtual
 * interfaces. */
#define OVN_QOS_MAX_RATE    34359738360ULL
static bool
add_ovs_qos_table_entry(struct ovsdb_idl_txn *ovs_idl_txn,
                        const struct ovsrec_port *port,
                        const struct ovsrec_interface *iface,
                        unsigned long long min_rate,
                        unsigned long long max_rate,
                        unsigned long long burst,
                        uint32_t queue_id, const char *ovn_port)
{
    struct smap external_ids = SMAP_INITIALIZER(&external_ids);
    struct smap other_config = SMAP_INITIALIZER(&other_config);

    const struct ovsrec_qos *qos = port->qos;
    if (qos && !smap_get_bool(&qos->external_ids, "ovn_qos", false)) {
        /* External configured QoS, do not overwrite it. */
        return false;
    }

    if (!qos) {
        qos = ovsrec_qos_insert(ovs_idl_txn);
        ovsrec_qos_set_type(qos, OVN_QOS_TYPE);
        ovsrec_port_set_qos(port, qos);

        const char *drv_name = smap_get_def(&iface->status, "driver_name", "");
        /* Link speed for virtual interfaces (e.g. veth or tap is inaccurate),
         * so use default value for them while rely on link speed for real
         * NICs. */
        if (!strcmp(drv_name, "veth") || !strcmp(drv_name, "tap") ||
            !iface->n_link_speed) {
            smap_add_format(&other_config, "max-rate", "%lld",
                            OVN_QOS_MAX_RATE);
        } else {
            smap_add_format(&other_config, "max-rate", "%lld",
                            (long long int) iface->link_speed[0]);
        }
        ovsrec_qos_set_other_config(qos, &other_config);
        smap_clear(&other_config);

        smap_add(&external_ids, "ovn_qos", "true");
        ovsrec_qos_set_external_ids(qos, &external_ids);
        smap_clear(&external_ids);
    }

    struct ovsrec_queue *queue;
    size_t i;
    for (i = 0; i < qos->n_queues; i++) {
        queue = qos->value_queues[i];

        const char *p = smap_get(&queue->external_ids, "ovn_port");
        if (p && !strcmp(p, ovn_port)) {
            break;
        }
    }

    if (i == qos->n_queues) {
        queue = ovsrec_queue_insert(ovs_idl_txn);
        ovsrec_qos_update_queues_setkey(qos, queue_id, queue);
    }

    smap_add_format(&other_config, "max-rate", "%llu", max_rate);
    smap_add_format(&other_config, "min-rate", "%llu", min_rate);
    smap_add_format(&other_config, "burst", "%llu", burst);
    ovsrec_queue_verify_other_config(queue);
    ovsrec_queue_set_other_config(queue, &other_config);
    smap_destroy(&other_config);

    smap_add(&external_ids, "ovn_port", ovn_port);
    ovsrec_queue_verify_external_ids(queue);
    ovsrec_queue_set_external_ids(queue, &external_ids);
    smap_destroy(&external_ids);
    return true;
}

static void
remove_stale_qos_entry( const char *logical_port,
                       struct ovsdb_idl_index *ovsrec_port_by_qos,
                       const struct ovsrec_qos_table *qos_table,
                       struct hmap *queue_map)
{
    struct qos_queue *q = find_qos_queue(
            queue_map, hash_string(logical_port, 0),
            logical_port);
    if (!q) {
        return;
    }

    const struct ovsrec_qos *qos;
    OVSREC_QOS_TABLE_FOR_EACH (qos, qos_table) {
        for (size_t i = 0; i < qos->n_queues; i++) {
            struct ovsrec_queue *queue = qos->value_queues[i];
            if (!queue) {
                continue;
            }

            const char *ovn_port = smap_get(
                    &queue->external_ids, "ovn_port");
            if (!ovn_port || strcmp(ovn_port, q->port)) {
                continue;
            }

            ovsrec_qos_update_queues_delkey(qos, qos->key_queues[i]);
            ovsrec_queue_delete(queue);

            if (qos->n_queues == 1) {
                const struct ovsrec_port *port =
                    ovsport_lookup_by_qos(ovsrec_port_by_qos, qos);
                if (port) {
                    ovsrec_port_set_qos(port, NULL);
                }
                ovsrec_qos_delete(qos);
            }

            hmap_remove(queue_map, &q->node);
            qos_queue_erase_entry(q);

            return;
        }
    }
}

static void
configure_qos(const struct sbrec_port_binding *pb,
              struct ovsdb_idl_txn *ovs_idl_txn,
              struct ovsdb_idl_index *ovsrec_port_by_qos,
              const struct ovsrec_qos_table *qos_table,
              struct hmap *qos_map,
              const struct ovsrec_open_vswitch_table *ovs_table,
              const struct ovsrec_bridge_table *bridge_table)
{
    unsigned long long min_rate = smap_get_ullong(
            &pb->options, "qos_min_rate", 0);
    unsigned long long max_rate = smap_get_ullong(
            &pb->options, "qos_max_rate", 0);
    unsigned long long burst = smap_get_ullong(
            &pb->options, "qos_burst", 0);
    uint32_t queue_id = smap_get_int(&pb->options, "qdisc_queue_id", 0);

    if ((!min_rate && !max_rate && !burst) || !queue_id) {
        /* Qos is not configured for this port. */
        remove_stale_qos_entry(pb->logical_port,
                               ovsrec_port_by_qos,
                               qos_table, qos_map);
        return;
    }

    const char *network = smap_get(&pb->options, "qos_physical_network");
    uint32_t hash = hash_string(pb->logical_port, 0);
    struct qos_queue *q = find_qos_queue(qos_map, hash,
                                         pb->logical_port);
    if (!q || q->min_rate != min_rate || q->max_rate != max_rate ||
        q->burst != burst || (network && strcmp(network, q->network))) {
        struct shash bridge_mappings = SHASH_INITIALIZER(&bridge_mappings);
        add_ovs_bridge_mappings(ovs_table, bridge_table,
                                &bridge_mappings);

        const struct ovsrec_port *port = NULL;
        const struct ovsrec_interface *iface = NULL;
        if (network) {
             iface = get_qos_egress_port_interface(&bridge_mappings, &port,
                                                   network);
        }
        if (iface) {
            /* Add new QoS entries. */
            if (add_ovs_qos_table_entry(ovs_idl_txn, port, iface,
                                        min_rate, max_rate, burst,
                                        queue_id, pb->logical_port)) {
                if (!q) {
                    q = xzalloc(sizeof *q);
                    hmap_insert(qos_map, &q->node, hash);
                    q->port = xstrdup(pb->logical_port);
                    q->queue_id = queue_id;
                }
                free(q->network);
                q->network = xstrdup(network);
                q->min_rate = min_rate;
                q->max_rate = max_rate;
                q->burst = burst;
            }
        }
        shash_destroy(&bridge_mappings);
    }
}

void
update_qos(struct ovsdb_idl_index *sbrec_port_binding_by_name,
           struct ovsdb_idl_txn *ovs_idl_txn,
           struct ovsdb_idl_index *ovsrec_port_by_qos,
           const struct ovsrec_qos_table *qos_table,
           struct hmap *qos_map,
           const struct ovsrec_open_vswitch_table *ovs_table,
           const struct ovsrec_bridge_table *bridge_table)
{
    /* Remove qos for any ports for which we could not do it before */
    const struct sbrec_port_binding *pb;

    struct shash_node *node;
    SHASH_FOR_EACH_SAFE (node, &_qos_ports) {
        struct qos_port *qos_port = (struct qos_port *) node->data;
        if (qos_port->added) {
            pb = lport_lookup_by_name(sbrec_port_binding_by_name,
                                      node->name);
            if (pb) {
                configure_qos(pb, ovs_idl_txn, ovsrec_port_by_qos, qos_table,
                              qos_map, ovs_table, bridge_table);
            }
        } else {
            remove_stale_qos_entry(node->name,
                                   ovsrec_port_by_qos,
                                   qos_table, qos_map);
        }
        free(qos_port);
        shash_delete(&_qos_ports, node);
    }
}

static const struct ovsrec_queue *
find_qos_queue_by_external_ids(const struct smap *external_ids,
    struct ovsdb_idl_index *ovsrec_queue_by_external_ids)
{
    const struct ovsrec_queue *queue =
        ovsrec_queue_index_init_row(ovsrec_queue_by_external_ids);
    ovsrec_queue_index_set_external_ids(queue, external_ids);
    const struct ovsrec_queue *retval =
        ovsrec_queue_index_find(ovsrec_queue_by_external_ids, queue);
    ovsrec_queue_index_destroy_row(queue);
    return retval;
}

static void
ovs_qos_entries_gc(struct ovsdb_idl_txn *ovs_idl_txn,
                   struct ovsdb_idl_index *ovsrec_port_by_qos,
                   struct ovsdb_idl_index *ovsrec_queue_by_external_ids,
                   const struct ovsrec_qos_table *qos_table,
                   struct hmap *queue_map)
{
    if (!ovs_idl_txn) {
        return;
    }

    const struct ovsrec_qos *qos, *qos_next;
    OVSREC_QOS_TABLE_FOR_EACH_SAFE (qos, qos_next, qos_table) {
        int n_queue_deleted = 0, n_queues = qos->n_queues;
        for (size_t i = 0; i < n_queues; i++) {
            struct ovsrec_queue *queue = qos->value_queues[i];
            if (!queue) {
                continue;
            }
            const struct ovsrec_queue *ovsrec_queue =
                find_qos_queue_by_external_ids(&queue->external_ids,
                                               ovsrec_queue_by_external_ids);
            if (!ovsrec_queue) {
                VLOG_DBG("queue already deleted !");
                continue;
            }

            const char *port = smap_get(&queue->external_ids, "ovn_port");
            if (!port) {
                continue;
            }

            struct qos_queue *q = find_qos_queue(queue_map,
                                                 hash_string(port, 0), port);
            if (!q) {
                ovsrec_qos_update_queues_delkey(qos, qos->key_queues[i]);
                ovsrec_queue_delete(queue);
                n_queue_deleted++;
            }
        }

        if (n_queue_deleted && n_queue_deleted == n_queues) {
            const struct ovsrec_port *port =
                ovsport_lookup_by_qos(ovsrec_port_by_qos, qos);
            if (port) {
                ovsrec_port_set_qos(port, NULL);
            }
            ovsrec_qos_delete(qos);
        }
    }
}

/*
 * Get the encap from the chassis for this port. The interface
 * may have an external_ids:encap-ip=<encap-ip> set; if so we
 * get the corresponding encap from the chassis.
 * If "encap-ip" external-ids is not set, we'll not bind the port
 * to any specific encap rec. and we'll pick up a tunnel port based on
 * the chassis name alone for the port.
 */
static struct sbrec_encap *
sbrec_get_port_encap(const struct sbrec_chassis *chassis_rec,
                     const struct ovsrec_interface *iface_rec)
{
    if (chassis_rec->n_encaps < 2) {
        return NULL;
    }

    const char *encap_ip = NULL;
    if (iface_rec) {
        encap_ip = smap_get(&iface_rec->external_ids, "encap-ip");
    }

    struct sbrec_encap *best_encap = NULL;
    uint32_t best_type = 0;
    for (int i = 0; i < chassis_rec->n_encaps; i++) {
        if ((encap_ip && !strcmp(chassis_rec->encaps[i]->ip, encap_ip)) ||
            (!encap_ip && smap_get_bool(&chassis_rec->encaps[i]->options,
                                        "is_default", false))) {
            uint32_t tun_type = get_tunnel_type(chassis_rec->encaps[i]->type);
            if (tun_type > best_type) {
                best_type = tun_type;
                best_encap = chassis_rec->encaps[i];
            }
        }
    }
    return best_encap;
}

static bool
is_network_plugged(const struct sbrec_port_binding *binding_rec,
                   struct shash *bridge_mappings)
{
    const char *network = smap_get(&binding_rec->options, "network_name");
    return network ? !!shash_find_data(bridge_mappings, network) : false;
}

static void
update_ld_external_ports(const struct sbrec_port_binding *binding_rec,
                         struct hmap *local_datapaths)
{
    struct local_datapath *ld = get_local_datapath(
        local_datapaths, binding_rec->datapath->tunnel_key);
    if (ld) {
        add_local_datapath_external_port(ld, binding_rec->logical_port,
                                         binding_rec);
    }
}

static void
update_ld_multichassis_ports(const struct sbrec_port_binding *binding_rec,
                             struct hmap *local_datapaths)
{
    struct local_datapath *ld = get_local_datapath(
        local_datapaths, binding_rec->datapath->tunnel_key);
    if (!ld) {
        return;
    }
    if (binding_rec->additional_chassis) {
        add_local_datapath_multichassis_port(ld, binding_rec->logical_port,
                                             binding_rec);
    } else {
        remove_local_datapath_multichassis_port(ld, binding_rec->logical_port);
    }
}

static void
update_ld_vtep_port(const struct sbrec_port_binding *binding_rec,
                    struct hmap *local_datapaths)
{
    struct local_datapath *ld
        = get_local_datapath(local_datapaths,
                             binding_rec->datapath->tunnel_key);
    if (!ld) {
        return;
    }

    if (ld->vtep_port && strcmp(ld->vtep_port->logical_port,
                                binding_rec->logical_port)) {
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
        VLOG_WARN_RL(&rl, "vtep port '%s' already set for datapath "
                     "'%"PRId64"', skipping the new port '%s'.",
                     ld->vtep_port->logical_port,
                     binding_rec->datapath->tunnel_key,
                     binding_rec->logical_port);
        return;
    }
    ld->vtep_port = binding_rec;
}

static void
update_ld_localnet_port(const struct sbrec_port_binding *binding_rec,
                        struct shash *bridge_mappings,
                        struct hmap *local_datapaths)
{
    /* Ignore localnet ports for unplugged networks. */
    if (!is_network_plugged(binding_rec, bridge_mappings)) {
        return;
    }

    struct local_datapath *ld
        = get_local_datapath(local_datapaths,
                             binding_rec->datapath->tunnel_key);
    if (!ld) {
        return;
    }

    if (ld->localnet_port && strcmp(ld->localnet_port->logical_port,
                                    binding_rec->logical_port)) {
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
        VLOG_WARN_RL(&rl, "localnet port '%s' already set for datapath "
                     "'%"PRId64"', skipping the new port '%s'.",
                     ld->localnet_port->logical_port,
                     binding_rec->datapath->tunnel_key,
                     binding_rec->logical_port);
        return;
    }
    ld->localnet_port = binding_rec;
}

/* Add an interface ID (usually taken from port_binding->name or
 * ovs_interface->external_ids:iface-id) to the set of local lports.
 * Also track if the set has changed.
 */
static void
update_local_lports(const char *iface_id, struct binding_ctx_out *b_ctx,
                    enum binding_local_lport_status status)
{
    struct simap_node *node = simap_find(b_ctx->local_lports, iface_id);
    if (node && node->data == status) {
        return;
    }
    if (node) {
        node->data = status;
    } else {
        simap_put(b_ctx->local_lports, iface_id, status);
    }
    b_ctx->local_lports_changed = true;
}

/* Remove an interface ID from the set of local lports. Also track if the
 * set has changed.
 */
static void
remove_local_lports(const char *iface_id, struct binding_ctx_out *b_ctx)
{
    if (simap_find_and_delete(b_ctx->local_lports, iface_id)) {
        b_ctx->local_lports_changed = true;
    }
}

/* Add a port binding to the set of locally relevant lports.
 * Also track if the set has changed.
 */
static void
update_related_lport(const struct sbrec_port_binding *pb,
                     struct binding_ctx_out *b_ctx)
{
    char buf[16];
    get_unique_lport_key(pb->datapath->tunnel_key, pb->tunnel_key,
                         buf, sizeof(buf));
    if (sset_add(&b_ctx->related_lports->lport_ids, buf) != NULL) {
        b_ctx->related_lports_changed = true;

        if (b_ctx->tracked_dp_bindings) {
            /* Add the 'pb' to the tracked_datapaths. */
            tracked_datapath_lport_add(pb, TRACKED_RESOURCE_NEW,
                                       b_ctx->tracked_dp_bindings);
        }
    }
    sset_add(&b_ctx->related_lports->lport_names, pb->logical_port);
}

/* Remove a port binding id from the set of locally relevant lports.
 * Also track if the set has changed.
 */
static void
remove_related_lport(const struct sbrec_port_binding *pb,
                     struct binding_ctx_out *b_ctx)
{
    char buf[16];
    get_unique_lport_key(pb->datapath->tunnel_key, pb->tunnel_key,
                         buf, sizeof(buf));
    sset_find_and_delete(&b_ctx->related_lports->lport_names,
                         pb->logical_port);
    if (sset_find_and_delete(&b_ctx->related_lports->lport_ids, buf)) {
        b_ctx->related_lports_changed = true;

        if (b_ctx->tracked_dp_bindings) {
            /* Add the 'pb' to the tracked_datapaths. */
            tracked_datapath_lport_add(pb, TRACKED_RESOURCE_REMOVED,
                                       b_ctx->tracked_dp_bindings);
        }
    }
}

/*
 * Update local_datapath peers when port type changed
 * and remove irrelevant ports from this list.
 */
static void
update_ld_peers(const struct sbrec_port_binding *pb,
                 struct hmap *local_datapaths)
{
    struct local_datapath *ld =
        get_local_datapath(local_datapaths, pb->datapath->tunnel_key);

    if (!ld) {
        return;
    }

    /*
     * This will handle cases where the pb type was explicitly
     * changed from router type to any other port type and will
     * remove it from the ld peers list.
     */
    enum en_lport_type type = get_lport_type(pb);
    size_t num_peers = vector_len(&ld->peer_ports);
    if (type != LP_PATCH) {
        remove_local_datapath_peer_port(pb, ld, local_datapaths);
        if (num_peers != vector_len(&ld->peer_ports)) {
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
            VLOG_DBG_RL(&rl,
                        "removing lport %s from the ld peers list",
                        pb->logical_port);
        }
    }
}

static void
delete_active_pb_ras_pd(const struct sbrec_port_binding *pb,
                        struct shash *ras_pd_map)
{
    shash_find_and_delete(ras_pd_map, pb->logical_port);
}

static void
update_active_pb_ras_pd(const struct sbrec_port_binding *pb,
                        struct shash *map, const char *conf)
{
    bool ras_pd_conf = smap_get_bool(&pb->options, conf, false);
    struct shash_node *iter = shash_find(map, pb->logical_port);

    if (!ras_pd_conf && iter) {
        shash_delete(map, iter);
    } else if (ras_pd_conf && !iter) {
        shash_add(map, pb->logical_port, pb);
    }
}

static bool is_ext_id_changed(const struct smap *a, const struct smap *b,
                              const char *key);
static struct local_binding *local_binding_create(
    const char *name, const struct ovsrec_interface *);
static void local_binding_add(struct shash *local_bindings,
                              struct local_binding *);
static void local_binding_destroy(struct local_binding *,
                                  struct shash *binding_lports);
static void local_binding_delete(struct local_binding *,
                                 struct shash *local_bindings,
                                 struct shash *binding_lports,
                                 struct if_status_mgr *if_mgr);
static struct binding_lport *local_binding_add_lport(
    struct shash *binding_lports,
    struct local_binding *,
    const struct sbrec_port_binding *,
    enum en_lport_type);
static struct binding_lport *local_binding_get_primary_lport(
    struct local_binding *);
static struct binding_lport *local_binding_get_first_lport(
    struct local_binding *lbinding);
static struct binding_lport *local_binding_get_primary_or_localport_lport(
    struct local_binding *lbinding);

static bool local_binding_handle_stale_binding_lports(
    struct local_binding *lbinding, struct binding_ctx_in *b_ctx_in,
    struct binding_ctx_out *b_ctx_out);

static struct binding_lport *binding_lport_create(
    const struct sbrec_port_binding *,
    struct local_binding *, enum en_lport_type);
static void binding_lport_destroy(struct binding_lport *);
static void binding_lport_delete(struct shash *binding_lports,
                                 struct binding_lport *);
static void binding_lport_add(struct shash *binding_lports,
                              struct binding_lport *);
static void binding_lport_set_up(struct binding_lport *, bool sb_readonly);
static void binding_lport_set_down(struct binding_lport *, bool sb_readonly);
static struct binding_lport *binding_lport_find(
    struct shash *binding_lports, const char *lport_name);
static const struct sbrec_port_binding *binding_lport_get_parent_pb(
    struct binding_lport *b_lprt);
static struct binding_lport *binding_lport_check_and_cleanup(
    struct binding_lport *, struct shash *b_lports);
static bool binding_lport_has_port_sec_changed(
    struct binding_lport *, const struct sbrec_port_binding *);
static void binding_lport_clear_port_sec(struct binding_lport *);
static bool binding_lport_update_port_sec(
    struct binding_lport *, const struct sbrec_port_binding *);

static bool ovs_iface_matches_lport_iface_id_ver(
    const struct ovsrec_interface *,
    const struct sbrec_port_binding *);

void
related_lports_init(struct related_lports *rp)
{
    sset_init(&rp->lport_names);
    sset_init(&rp->lport_ids);
}

void
related_lports_destroy(struct related_lports *rp)
{
    sset_destroy(&rp->lport_names);
    sset_destroy(&rp->lport_ids);
}

void
local_binding_data_init(struct local_binding_data *lbinding_data)
{
    shash_init(&lbinding_data->bindings);
    shash_init(&lbinding_data->lports);
}

void
local_binding_data_destroy(struct local_binding_data *lbinding_data)
{
    struct shash_node *node;

    SHASH_FOR_EACH_SAFE (node, &lbinding_data->lports) {
        struct binding_lport *b_lport = node->data;
        binding_lport_destroy(b_lport);
        shash_delete(&lbinding_data->lports, node);
    }

    SHASH_FOR_EACH_SAFE (node, &lbinding_data->bindings) {
        struct local_binding *lbinding = node->data;
        local_binding_destroy(lbinding, &lbinding_data->lports);
        shash_delete(&lbinding_data->bindings, node);
    }

    shash_destroy(&lbinding_data->lports);
    shash_destroy(&lbinding_data->bindings);
}

const struct sbrec_port_binding *
local_binding_get_primary_pb(const struct shash *local_bindings,
                             const char *pb_name)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    struct binding_lport *b_lport = local_binding_get_primary_lport(lbinding);

    return b_lport ? b_lport->pb : NULL;
}

ofp_port_t
local_binding_get_lport_ofport(const struct shash *local_bindings,
                               const char *pb_name)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    struct binding_lport *b_lport =
        local_binding_get_primary_or_localport_lport(lbinding);

    return (b_lport && lbinding->iface && lbinding->iface->n_ofport) ?
            u16_to_ofp(lbinding->iface->ofport[0]) : 0;
}

bool
local_binding_is_ovn_installed(struct shash *local_bindings,
                               const char *pb_name)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    if (lbinding && lbinding->iface) {
        return smap_get_bool(&lbinding->iface->external_ids,
                             OVN_INSTALLED_EXT_ID, false);
    }
    return false;
}

bool
local_binding_is_up(struct shash *local_bindings, const char *pb_name,
                    const struct sbrec_chassis *chassis_rec)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    struct binding_lport *b_lport = local_binding_get_primary_lport(lbinding);

    if (b_lport && b_lport->pb->chassis != chassis_rec) {
        return false;
    }

    if (lbinding && b_lport && lbinding->iface) {
        if (b_lport->pb->n_up && !b_lport->pb->up[0]) {
            return false;
        }
        return smap_get_bool(&lbinding->iface->external_ids,
                             OVN_INSTALLED_EXT_ID, false);
    }
    return false;
}

bool
local_binding_is_down(struct shash *local_bindings, const char *pb_name,
                      const struct sbrec_chassis *chassis_rec)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);

    struct binding_lport *b_lport = local_binding_get_primary_lport(lbinding);

    if (b_lport) {
        if (b_lport->pb->chassis == chassis_rec) {
            return false;
        } else if (b_lport->pb->chassis) {
            VLOG_DBG("lport %s already claimed by other chassis",
                     b_lport->pb->logical_port);
            return true;
        }
    }

    if (!lbinding) {
        return true;
    }

    if (lbinding->iface && smap_get_bool(&lbinding->iface->external_ids,
                                         OVN_INSTALLED_EXT_ID, false)) {
        return false;
    }

    if (b_lport && b_lport->pb->n_up && b_lport->pb->up[0]) {
        return false;
    }

    return true;
}

void
local_binding_set_up(struct shash *local_bindings, const char *pb_name,
                     const struct sbrec_chassis *chassis_rec,
                     const char *ts_now_str, bool sb_readonly,
                     bool ovs_readonly)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    struct binding_lport *b_lport = local_binding_get_primary_lport(lbinding);

    if (!ovs_readonly && lbinding && lbinding->iface
            && !smap_get_bool(&lbinding->iface->external_ids,
                              OVN_INSTALLED_EXT_ID, false)) {
        VLOG_INFO("Setting lport %s ovn-installed in OVS", pb_name);
        ovsrec_interface_update_external_ids_setkey(lbinding->iface,
                                                    OVN_INSTALLED_EXT_ID,
                                                    "true");
        ovsrec_interface_update_external_ids_setkey(lbinding->iface,
                                                    OVN_INSTALLED_TS_EXT_ID,
                                                    ts_now_str);
    }

    if (!sb_readonly && lbinding && b_lport && b_lport->pb->n_up &&
            !b_lport->pb->up[0] && b_lport->pb->chassis == chassis_rec) {
        binding_lport_set_up(b_lport, sb_readonly);
        LIST_FOR_EACH (b_lport, list_node, &lbinding->binding_lports) {
            binding_lport_set_up(b_lport, sb_readonly);
        }
    }
}

void
local_binding_remove_ovn_installed(
        struct shash *local_bindings,
        const struct ovsrec_interface_table *iface_table,
        const char *pb_name, bool ovs_readonly)
{
    if (ovs_readonly) {
        return;
    }
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    if (lbinding && lbinding->iface) {
        const struct uuid *iface_uuid = &lbinding->iface->header_.uuid;
        remove_ovn_installed_for_uuid(iface_table, iface_uuid);
    }
}

void
remove_ovn_installed_for_uuid(const struct ovsrec_interface_table *iface_table,
                              const struct uuid *iface_uuid)
{
    const struct ovsrec_interface *iface_rec =
        ovsrec_interface_table_get_for_uuid(iface_table, iface_uuid);
    if (iface_rec && smap_get_bool(&iface_rec->external_ids,
                                   OVN_INSTALLED_EXT_ID, false)) {
        VLOG_INFO("Removing iface %s ovn-installed in OVS",
                  iface_rec->name);
        ovsrec_interface_update_external_ids_delkey(iface_rec,
                                                    OVN_INSTALLED_EXT_ID);
    }
}

void
local_binding_set_down(struct shash *local_bindings, const char *pb_name,
                       const struct sbrec_chassis *chassis_rec,
                       bool sb_readonly, bool ovs_readonly)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    struct binding_lport *b_lport = local_binding_get_primary_lport(lbinding);

    if (!ovs_readonly && lbinding && lbinding->iface
            && smap_get_bool(&lbinding->iface->external_ids,
                             OVN_INSTALLED_EXT_ID, false)) {
        VLOG_INFO("Removing lport %s ovn-installed in OVS", pb_name);
        ovsrec_interface_update_external_ids_delkey(lbinding->iface,
                                                    OVN_INSTALLED_EXT_ID);
    }

    if (!sb_readonly && b_lport && b_lport->pb->n_up && b_lport->pb->up[0] &&
            (!b_lport->pb->chassis || b_lport->pb->chassis == chassis_rec)) {
        binding_lport_set_down(b_lport, sb_readonly);
        LIST_FOR_EACH (b_lport, list_node, &lbinding->binding_lports) {
            binding_lport_set_down(b_lport, sb_readonly);
        }
    }
}

void
binding_dump_related_lports(struct related_lports *related_lports,
                            struct ds *out_data)
{
    const char *name;
    SSET_FOR_EACH (name, &related_lports->lport_names) {
        ds_put_format(out_data, "%s\n", name);
    }
}

void
binding_dump_local_bindings(struct local_binding_data *lbinding_data,
                            struct ds *out_data)
{
    const struct shash_node **nodes;

    nodes = shash_sort(&lbinding_data->bindings);
    size_t n = shash_count(&lbinding_data->bindings);

    ds_put_cstr(out_data, "Local bindings:\n");
    for (size_t i = 0; i < n; i++) {
        const struct shash_node *node = nodes[i];
        struct local_binding *lbinding = node->data;
        size_t num_lports = ovs_list_size(&lbinding->binding_lports);
        ds_put_format(out_data, "name: [%s], OVS interface name : [%s], "
                      "num binding lports : [%"PRIuSIZE"]\n",
                      lbinding->name,
                      lbinding->iface ? lbinding->iface->name : "NULL",
                      num_lports);

        if (num_lports) {
            struct shash child_lports = SHASH_INITIALIZER(&child_lports);
            struct binding_lport *primary_lport = NULL;
            struct binding_lport *localport_lport = NULL;
            struct binding_lport *b_lport;
            bool first_elem = true;

            LIST_FOR_EACH (b_lport, list_node, &lbinding->binding_lports) {
                if (first_elem && b_lport->type == LP_VIF) {
                    primary_lport = b_lport;
                } else if (first_elem && b_lport->type == LP_LOCALPORT) {
                    localport_lport = b_lport;
                } else {
                    shash_add(&child_lports, b_lport->name, b_lport);
                }
                first_elem = false;
            }

            if (primary_lport) {
                ds_put_format(out_data, "primary lport : [%s]\n",
                              primary_lport->name);
            } else if (localport_lport) {
                ds_put_format(out_data, "localport lport : [%s]\n",
                              localport_lport->name);
            } else {
                ds_put_format(out_data, "no primary lport\n");
            }

            if (!shash_is_empty(&child_lports)) {
                const struct shash_node **c_nodes =
                    shash_sort(&child_lports);
                for (size_t j = 0; j < shash_count(&child_lports); j++) {
                    b_lport = c_nodes[j]->data;
                    ds_put_format(out_data, "child lport[%"PRIuSIZE"] : [%s], "
                                  "type : [%s]\n", j + 1, b_lport->name,
                                  get_lport_type_str(b_lport->type));
                }
                free(c_nodes);
            }
            shash_destroy(&child_lports);
        }

        ds_put_cstr(out_data, "----------------------------------------\n");
    }

    free(nodes);
}

void
binding_dump_local_datapaths(struct hmap *local_datapaths,
                             struct ds *out_data)
{
    ds_put_cstr(out_data, "Local datapaths:\n");
    struct local_datapath *ld;
    HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
        ds_put_format(out_data, "Datapath: %s, type: %s\n",
                      smap_get(&ld->datapath->external_ids, "name"),
                      ld->is_switch ? "switch" : "router");
    }
}

void
set_pb_chassis_in_sbrec(const struct sbrec_port_binding *pb,
                        const struct sbrec_chassis *chassis_rec,
                        bool is_set)
{
    if (pb->chassis != chassis_rec) {
         if (is_set) {
            if (pb->chassis) {
                VLOG_INFO("Changing chassis for lport %s from %s to %s.",
                          pb->logical_port, pb->chassis->name,
                          chassis_rec->name);
            } else {
                VLOG_INFO("Claiming lport %s for this chassis.",
                          pb->logical_port);
            }
            for (int i = 0; i < pb->n_mac; i++) {
                VLOG_INFO("%s: Claiming %s", pb->logical_port, pb->mac[i]);
            }
            sbrec_port_binding_set_chassis(pb, chassis_rec);
        }
    } else if (!is_set) {
        sbrec_port_binding_set_chassis(pb, NULL);
    }
}

void
set_pb_additional_chassis_in_sbrec(const struct sbrec_port_binding *pb,
                                   const struct sbrec_chassis *chassis_rec,
                                   bool is_set)
{
    if (!is_additional_chassis(pb, chassis_rec)) {
        VLOG_INFO("Claiming lport %s for this additional chassis.",
                  pb->logical_port);
        for (size_t i = 0; i < pb->n_mac; i++) {
            VLOG_INFO("%s: Claiming %s", pb->logical_port, pb->mac[i]);
        }
        sbrec_port_binding_update_additional_chassis_addvalue(pb, chassis_rec);
        if (pb->chassis == chassis_rec) {
            sbrec_port_binding_set_chassis(pb, NULL);
        }
    } else if (!is_set) {
        remove_additional_chassis(pb, chassis_rec);
    }
}

bool
local_bindings_pb_chassis_is_set(struct shash *local_bindings,
                                 const char *pb_name,
                                 const struct sbrec_chassis *chassis_rec)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    struct binding_lport *b_lport = local_binding_get_primary_lport(lbinding);

    if (b_lport && b_lport->pb &&
       ((b_lport->pb->chassis == chassis_rec) ||
         is_additional_chassis(b_lport->pb, chassis_rec))) {
        return true;
    }
    return false;
}

void
local_binding_set_pb(struct shash *local_bindings, const char *pb_name,
                     const struct sbrec_chassis *chassis_rec,
                     struct hmap *tracked_datapaths, bool is_set,
                     enum can_bind bind_type)
{
    struct local_binding *lbinding =
        local_binding_find(local_bindings, pb_name);
    struct binding_lport *b_lport = local_binding_get_primary_lport(lbinding);

    if (b_lport) {
        if (bind_type == CAN_BIND_AS_MAIN) {
            set_pb_chassis_in_sbrec(b_lport->pb, chassis_rec, is_set);
        } else  if (bind_type == CAN_BIND_AS_ADDITIONAL) {
            set_pb_additional_chassis_in_sbrec(b_lport->pb, chassis_rec,
                                               is_set);
        }
        if (tracked_datapaths) {
            update_lport_tracking(b_lport->pb, tracked_datapaths, true);
        }
    }
}

/* For newly claimed ports:
 * - set the 'pb.up' field to true if 'pb' has no 'parent_pb'.
 * - set the 'pb.up' field to true if 'parent_pb.up' is 'true' (e.g., for
 *   container and virtual ports).
 *
 * Note:
 *   Updates the 'pb->up' field only if it's explicitly set to 'false'.
 *   This is to ensure compatibility with older versions of ovn-northd.
 */
void
claimed_lport_set_up(const struct sbrec_port_binding *pb,
                     const struct sbrec_port_binding *parent_pb)
{
    bool up = true;
    if (!parent_pb || (parent_pb->n_up && parent_pb->up[0])) {
        if (pb->n_up && !pb->up[0]) {
            VLOG_INFO("Setting lport %s up in Southbound",
                      pb->logical_port);
            sbrec_port_binding_set_up(pb, &up, 1);
        }
    }
}

typedef void (*set_func)(const struct sbrec_port_binding *pb,
                         const struct sbrec_encap *);

static bool
update_port_encap_if_needed(const struct sbrec_port_binding *pb,
                            const struct sbrec_chassis *chassis_rec,
                            const struct ovsrec_interface *iface_rec,
                            bool sb_readonly)
{
    const struct sbrec_encap *encap_rec =
        sbrec_get_port_encap(chassis_rec, iface_rec);
    if ((encap_rec && pb->encap != encap_rec) ||
        (!encap_rec && pb->encap)) {
        if (sb_readonly) {
            return false;
        }
        sbrec_port_binding_set_encap(pb, encap_rec);
    }
    return true;
}

static void
remove_additional_encap_for_chassis(const struct sbrec_port_binding *pb,
                                    const struct sbrec_chassis *chassis_rec)
{
    for (size_t i = 0; i < pb->n_additional_encap; i++) {
        if (!strcmp(pb->additional_encap[i]->chassis_name,
                    chassis_rec->name)) {
            sbrec_port_binding_update_additional_encap_delvalue(
                pb, pb->additional_encap[i]);
        }
    }
}

static bool
update_port_additional_encap_if_needed(
    const struct sbrec_port_binding *pb,
    const struct sbrec_chassis *chassis_rec,
    const struct ovsrec_interface *iface_rec,
    bool sb_readonly)
{
    const struct sbrec_encap *encap_rec =
        sbrec_get_port_encap(chassis_rec, iface_rec);
    if (encap_rec) {
        for (size_t i = 0; i < pb->n_additional_encap; i++) {
            if (pb->additional_encap[i] == encap_rec) {
                return true;
            }
        }
        if (sb_readonly) {
            return false;
        }
        sbrec_port_binding_update_additional_encap_addvalue(pb, encap_rec);
    }
    return true;
}

static bool
is_requested_additional_chassis(const struct sbrec_port_binding *pb,
                      const struct sbrec_chassis *chassis_rec)
{
    for (size_t i = 0; i < pb->n_requested_additional_chassis; i++) {
        if (pb->requested_additional_chassis[i] == chassis_rec) {
            return true;
        }
    }
    return false;
}

bool
is_additional_chassis(const struct sbrec_port_binding *pb,
                      const struct sbrec_chassis *chassis_rec)
{
    for (size_t i = 0; i < pb->n_additional_chassis; i++) {
        if (pb->additional_chassis[i] == chassis_rec) {
            return true;
        }
    }
    return false;
}

static void
remove_additional_chassis(const struct sbrec_port_binding *pb,
                          const struct sbrec_chassis *chassis_rec)
{
    sbrec_port_binding_update_additional_chassis_delvalue(pb, chassis_rec);
    remove_additional_encap_for_chassis(pb, chassis_rec);
}

bool
lport_maybe_postpone(const struct sbrec_port_binding *pb,
                     const struct sbrec_chassis *chassis_rec,
                     long long int now,
                     struct sset *postponed_ports)
{
    if (pb->ha_chassis_group) {
        struct ha_chassis_ordered *ordered_ha_ch =
            get_ordered_ha_chassis_list(pb->ha_chassis_group, NULL,
                                        chassis_rec);
        if (ordered_ha_ch &&
            ordered_ha_ch->ha_ch[0].chassis == chassis_rec) {
            ha_chassis_destroy_ordered(ordered_ha_ch);
            return false;
        } else {
            ha_chassis_destroy_ordered(ordered_ha_ch);
        }
    }
    long long int last_claimed = get_claim_timestamp(pb->logical_port);
    if (now - last_claimed >= CLAIM_TIME_THRESHOLD_MS) {
        return false;
    }

    sset_add(postponed_ports, pb->logical_port);
    VLOG_DBG("Postponed claim on logical port %s.", pb->logical_port);

    return true;
}

static bool
is_postponed_port(const char *port_name)
{
    return sset_contains(&_postponed_ports, port_name);
}

/* Returns false if lport is not claimed due to 'sb_readonly'.
 * Returns true otherwise.
 */
static bool
claim_lport(const struct sbrec_port_binding *pb,
            const struct sbrec_port_binding *parent_pb,
            const struct sbrec_chassis *chassis_rec,
            const struct ovsrec_interface *iface_rec,
            bool sb_readonly, bool is_vif,
            struct hmap *tracked_datapaths,
            struct if_status_mgr *if_mgr,
            struct sset *postponed_ports,
            enum can_bind can_bind)
{
    bool update_tracked = false;

    if (can_bind == CAN_BIND_AS_MAIN) {
        if (pb->chassis != chassis_rec) {
            long long int now = time_msec();
            if (pb->chassis) {
                if (lport_maybe_postpone(pb, chassis_rec, now,
                                         postponed_ports)) {
                    return true;
                }
            }
            if (is_additional_chassis(pb, chassis_rec)) {
                if (sb_readonly) {
                    return false;
                }
                remove_additional_chassis(pb, chassis_rec);
            }
            update_tracked = true;

            if_status_mgr_claim_iface(if_mgr, pb, chassis_rec, iface_rec,
                                      sb_readonly, can_bind, is_vif,
                                      parent_pb);
            register_claim_timestamp(pb->logical_port, now);
            sset_find_and_delete(postponed_ports, pb->logical_port);
        } else {
            update_tracked = true;
            if ((pb->n_up && !pb->up[0]) ||
                (is_vif && !smap_get_bool(&iface_rec->external_ids,
                                          OVN_INSTALLED_EXT_ID, false))) {
                if_status_mgr_claim_iface(if_mgr, pb, chassis_rec,
                                          iface_rec, sb_readonly,
                                          can_bind, is_vif,
                                          parent_pb);
            }
        }
    } else if (can_bind == CAN_BIND_AS_ADDITIONAL) {
        if (!is_additional_chassis(pb, chassis_rec)) {
            if_status_mgr_claim_iface(if_mgr, pb, chassis_rec, iface_rec,
                                      sb_readonly, can_bind, is_vif,
                                      parent_pb);
            update_tracked = true;
        }
    }

    if (update_tracked) {
        if (tracked_datapaths) {
            update_lport_tracking(pb, tracked_datapaths, true);
        }
    }

    /* Check if the port encap binding, if any, has changed */
    if (can_bind == CAN_BIND_AS_MAIN) {
        return update_port_encap_if_needed(
            pb, chassis_rec, iface_rec, sb_readonly);
    } else if (can_bind == CAN_BIND_AS_ADDITIONAL) {
        return update_port_additional_encap_if_needed(
            pb, chassis_rec, iface_rec, sb_readonly);
    }
    return true;
}

/* Returns false if lport is not released due to 'sb_readonly'.
 * Returns true otherwise.
 *
 * This function assumes that the the 'pb' was claimed
 * earlier i.e port binding's chassis is set to this chassis.
 * Caller should make sure that this is the case.
 */
static bool
release_lport_main_chassis(const struct sbrec_port_binding *pb,
                           bool sb_readonly,
                           struct if_status_mgr *if_mgr)
{
    if (pb->encap) {
        if (sb_readonly) {
            return false;
        }
        sbrec_port_binding_set_encap(pb, NULL);
    }

    /* If sb is readonly, pb->chassis is unset through if-status if present. */

    if (pb->chassis) {
        if (!sb_readonly) {
            sbrec_port_binding_set_chassis(pb, NULL);
        } else if (!if_status_mgr_iface_is_present(if_mgr, pb->logical_port)) {
            return false;
        }
    }

    if (pb->virtual_parent) {
        if (sb_readonly) {
            return false;
        }
        sbrec_port_binding_set_virtual_parent(pb, NULL);
    }

    VLOG_INFO("Releasing lport %s from this chassis (sb_readonly=%d)",
              pb->logical_port, sb_readonly);

    return true;
}

static bool
release_lport_additional_chassis(const struct sbrec_port_binding *pb,
                                 const struct sbrec_chassis *chassis_rec,
                                 bool sb_readonly)
{
    if (pb->additional_encap) {
        if (sb_readonly) {
            return false;
        }
        remove_additional_encap_for_chassis(pb, chassis_rec);
    }

    if (is_additional_chassis(pb, chassis_rec)) {
        if (sb_readonly) {
            return false;
        }
        remove_additional_chassis(pb, chassis_rec);
    }

    VLOG_INFO("Releasing lport %s from this additional chassis.",
              pb->logical_port);
    return true;
}

static bool
release_lport(const struct sbrec_port_binding *pb,
              const struct sbrec_chassis *chassis_rec, bool sb_readonly,
              struct hmap *tracked_datapaths, struct if_status_mgr *if_mgr)
{
    if (pb->chassis == chassis_rec) {
        if (!release_lport_main_chassis(pb, sb_readonly, if_mgr)) {
            return false;
        }
    } else if (is_additional_chassis(pb, chassis_rec)) {
        if (!release_lport_additional_chassis(pb, chassis_rec, sb_readonly)) {
            return false;
        }
    } else {
        VLOG_INFO("Releasing lport %s", pb->logical_port);
    }
    update_lport_tracking(pb, tracked_datapaths, false);
    if_status_mgr_release_iface(if_mgr, pb->logical_port);
    return true;
}

static bool
is_lbinding_set(struct local_binding *lbinding)
{
    return lbinding && lbinding->iface;
}

static bool
is_binding_lport_this_chassis(struct binding_lport *b_lport,
                              const struct sbrec_chassis *chassis)
{
    return (b_lport && b_lport->pb && chassis &&
            (b_lport->pb->chassis == chassis
             || is_additional_chassis(b_lport->pb, chassis)
             || is_postponed_port(b_lport->pb->logical_port)));
}

/* Returns 'true' if the 'lbinding' has binding lports of type
 * LP_CONTAINER/LP_MIRROR, 'false' otherwise. */
static bool
is_lbinding_container_parent(struct local_binding *lbinding)
{
    struct binding_lport *b_lport;
    LIST_FOR_EACH (b_lport, list_node, &lbinding->binding_lports) {
        if (b_lport->type == LP_CONTAINER ||
            b_lport->type == LP_MIRROR) {
            return true;
        }
    }

    return false;
}

static bool
release_binding_lport(const struct sbrec_chassis *chassis_rec,
                      struct binding_lport *b_lport, bool sb_readonly,
                      struct binding_ctx_out *b_ctx_out)
{
    if (b_lport && b_lport->pb) {
        remove_related_lport(b_lport->pb, b_ctx_out);
    }
    if (is_binding_lport_this_chassis(b_lport, chassis_rec)) {
        if (!release_lport(b_lport->pb, chassis_rec, sb_readonly,
                           b_ctx_out->tracked_dp_bindings,
                           b_ctx_out->if_mgr)) {
            return false;
        }
        binding_lport_set_down(b_lport, sb_readonly);
    }

    return true;
}

static bool
consider_vif_lport_(const struct sbrec_port_binding *pb,
                    enum can_bind can_bind,
                    struct binding_ctx_in *b_ctx_in,
                    struct binding_ctx_out *b_ctx_out,
                    struct binding_lport *b_lport)
{
    bool lbinding_set = b_lport && is_lbinding_set(b_lport->lbinding);

    if (lbinding_set) {
        if (can_bind) {
            /* We can claim the lport. */
            const struct sbrec_port_binding *parent_pb =
                binding_lport_get_parent_pb(b_lport);

            if (!claim_lport(pb, parent_pb, b_ctx_in->chassis_rec,
                             b_lport->lbinding->iface,
                             !b_ctx_in->ovnsb_idl_txn,
                             !parent_pb, b_ctx_out->tracked_dp_bindings,
                             b_ctx_out->if_mgr,
                             b_ctx_out->postponed_ports, can_bind)) {
                return false;
            }

            add_local_datapath(b_ctx_in->sbrec_datapath_binding_by_key,
                               b_ctx_in->sbrec_port_binding_by_datapath,
                               b_ctx_in->sbrec_port_binding_by_name,
                               pb->datapath, b_ctx_in->chassis_rec,
                               b_ctx_out->local_datapaths,
                               b_ctx_out->tracked_dp_bindings);
            update_related_lport(pb, b_ctx_out);
            update_local_lports(pb->logical_port, b_ctx_out,
                                LPORT_STATUS_BOUND);
            if (binding_lport_update_port_sec(b_lport, pb) &&
                    b_ctx_out->tracked_dp_bindings) {
                tracked_datapath_lport_add(pb, TRACKED_RESOURCE_UPDATED,
                                           b_ctx_out->tracked_dp_bindings);
            }
            if (b_lport->lbinding->iface) {
                add_or_del_qos_port(pb->logical_port, true);
            }
        } else {
            /* We could, but can't claim the lport. */
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
            const char *requested_chassis_option = smap_get(
                &pb->options, "requested-chassis");
            VLOG_INFO_RL(&rl,
                "Not claiming lport %s, chassis %s requested-chassis %s "
                "pb->chassis %s",
                pb->logical_port, b_ctx_in->chassis_rec->name,
                requested_chassis_option ? requested_chassis_option : "[]",
                pb->chassis ? pb->chassis->name: "");
        }
    }

    if (pb->chassis == b_ctx_in->chassis_rec
            || is_additional_chassis(pb, b_ctx_in->chassis_rec)
            || if_status_is_port_claimed(b_ctx_out->if_mgr,
                                         pb->logical_port)) {
        /* Release the lport if there is no lbinding. */
       if (lbinding_set && !can_bind) {
            if_status_mgr_remove_ovn_installed(b_ctx_out->if_mgr,
                                               b_lport->lbinding->iface);
        }

        if (!lbinding_set || !can_bind) {
            remove_related_lport(pb, b_ctx_out);
            return release_lport(pb, b_ctx_in->chassis_rec,
                                 !b_ctx_in->ovnsb_idl_txn,
                                 b_ctx_out->tracked_dp_bindings,
                                 b_ctx_out->if_mgr);
        }
    }

    struct binding_lport *parent_b_lport = b_lport && b_lport->lbinding ?
        local_binding_get_primary_lport(b_lport->lbinding) : NULL;

    if (pb->chassis && pb->chassis != b_ctx_in->chassis_rec
            && !is_requested_additional_chassis(pb, b_ctx_in->chassis_rec)
            && (!parent_b_lport
                || !is_requested_additional_chassis(parent_b_lport->pb,
                                                    b_ctx_in->chassis_rec))
            && if_status_is_port_claimed(b_ctx_out->if_mgr,
                                         pb->logical_port)) {
        update_lport_tracking(pb, b_ctx_out->tracked_dp_bindings, false);
        if_status_mgr_remove_ovn_installed(b_ctx_out->if_mgr,
                                           b_lport->lbinding->iface);
    }

    return true;
}

static bool
consider_vif_lport(const struct sbrec_port_binding *pb,
                   struct binding_ctx_in *b_ctx_in,
                   struct binding_ctx_out *b_ctx_out,
                   struct local_binding *lbinding)
{
    enum can_bind can_bind =
        lport_can_bind_on_this_chassis(b_ctx_in->chassis_rec, pb);

    if (!lbinding) {
        lbinding = local_binding_find(&b_ctx_out->lbinding_data->bindings,
                                      pb->logical_port);
    }

    struct binding_lport *b_lport = NULL;
    if (lbinding) {
        /* Make sure that the pb's iface-id-ver if set matches with the
         * lbinding ovs iface's iface-id-ver. */
        if (lbinding->iface &&
                !ovs_iface_matches_lport_iface_id_ver(lbinding->iface, pb)) {
            /* We can't associate the b_lport for this local_binding
             * because the iface-id-ver doesn't match.  Check if there is
             * a primary lport for this lbinding.  If so, delete it. */
            b_lport = local_binding_get_primary_lport(lbinding);
            if (b_lport) {
                binding_lport_delete(&b_ctx_out->lbinding_data->lports,
                                     b_lport);
                b_lport = NULL;
            }
        } else {
            struct shash *binding_lports =
                &b_ctx_out->lbinding_data->lports;
            b_lport = local_binding_add_lport(binding_lports, lbinding, pb,
                                              LP_VIF);
        }
    }

    return consider_vif_lport_(pb, can_bind, b_ctx_in, b_ctx_out, b_lport);
}

static bool
consider_container_lport(const struct sbrec_port_binding *pb,
                         enum en_lport_type type,
                         struct binding_ctx_in *b_ctx_in,
                         struct binding_ctx_out *b_ctx_out)
{
    struct shash *local_bindings = &b_ctx_out->lbinding_data->bindings;
    struct local_binding *parent_lbinding;
    const char *binding_port_name = (type == LP_MIRROR) ? pb->mirror_port :
                                    pb->parent_port;

    parent_lbinding = local_binding_find(local_bindings, binding_port_name);

    if (!parent_lbinding) {
        /* There is no local_binding for parent port. Create it
         * without OVS interface row. This is the only exception
         * for creating the 'struct local_binding' object without
         * corresponding OVS interface row.
         *
         * This is required for the following reasons:
         *   - If a logical port P1 is created and then
         *     few container ports - C1, C2, .. are created first by CMS.
         *   - And later when OVS interface row  is created for P1, then
         *     we want the these container ports also be claimed by the
         *     chassis.
         * */
        parent_lbinding = local_binding_create(binding_port_name, NULL);
        local_binding_add(local_bindings, parent_lbinding);
    }

    struct shash *binding_lports = &b_ctx_out->lbinding_data->lports;
    struct binding_lport *b_lport =
        binding_lport_find(binding_lports, pb->logical_port);

    if (b_lport && b_lport->lbinding != parent_lbinding) {
        /* The container lport's parent has changed.  So remove it from
         * the related_lports so that it is tracked. */
        remove_related_lport(b_lport->pb, b_ctx_out);
    }

    struct binding_lport *container_b_lport;

    if (type == LP_MIRROR) {
        container_b_lport = local_binding_add_lport(binding_lports,
                                                    parent_lbinding,
                                                    pb, LP_MIRROR);
    } else {
        container_b_lport = local_binding_add_lport(binding_lports,
                                                    parent_lbinding,
                                                    pb, LP_CONTAINER);
    }

    struct binding_lport *parent_b_lport =
        binding_lport_find(binding_lports, binding_port_name);

    bool can_consider_c_lport = true;
    if (!parent_b_lport || !parent_b_lport->pb) {
        const struct sbrec_port_binding *parent_pb = lport_lookup_by_name(
            b_ctx_in->sbrec_port_binding_by_name, binding_port_name);

        if (parent_pb && get_lport_type(parent_pb) == LP_VIF) {
            /* Its possible that the parent lport is not considered yet.
             * So call consider_vif_lport() to process it first. */
            consider_vif_lport(parent_pb, b_ctx_in, b_ctx_out,
                               parent_lbinding);
            parent_b_lport = binding_lport_find(binding_lports,
                                                binding_port_name);
        } else {
            /* The parent lport doesn't exist.  Cannot consider the container
             * lport for binding. */
            can_consider_c_lport = false;
        }
    }

    if (parent_b_lport && parent_b_lport->type != LP_VIF) {
        can_consider_c_lport = false;
    }

    if (!can_consider_c_lport) {
        /* Call release_lport() to release the container lport,
         * if it was bound earlier. */
        if (is_binding_lport_this_chassis(container_b_lport,
                                          b_ctx_in->chassis_rec)) {
            return release_lport(pb, b_ctx_in->chassis_rec,
                                 !b_ctx_in->ovnsb_idl_txn,
                                 b_ctx_out->tracked_dp_bindings,
                                 b_ctx_out->if_mgr);
        }

        return true;
    }

    ovs_assert(parent_b_lport && parent_b_lport->pb);
    /* cannot bind to this chassis if the parent_port/mirror_port
     * cannot be bounded. */
    /* Do not bind neither if parent is postponed */

    enum can_bind can_bind =
        (!is_postponed_port(parent_b_lport->pb->logical_port) &&
         lport_can_bind_on_this_chassis(b_ctx_in->chassis_rec, pb)) ?
         lport_can_bind_on_this_chassis(b_ctx_in->chassis_rec,
                                        parent_b_lport->pb) : CANNOT_BIND;

    return consider_vif_lport_(pb, can_bind, b_ctx_in, b_ctx_out,
                               container_b_lport);
}

static bool
consider_virtual_lport(const struct sbrec_port_binding *pb,
                       struct binding_ctx_in *b_ctx_in,
                       struct binding_ctx_out *b_ctx_out)
{
    struct shash *local_bindings = &b_ctx_out->lbinding_data->bindings;
    struct local_binding *parent_lbinding =
        pb->virtual_parent ? local_binding_find(local_bindings,
                                                pb->virtual_parent)
        : NULL;

    struct binding_lport *virtual_b_lport = NULL;
    /* Unlike container lports, we don't have to create parent_lbinding if
     * it is NULL. This is because, if parent_lbinding is not present, it
     * means the virtual port can't bind in this chassis.
     * Note: pinctrl module binds the virtual lport when it sees ARP
     * packet from the parent lport. */
    if (parent_lbinding) {
        struct shash *binding_lports = &b_ctx_out->lbinding_data->lports;

        struct binding_lport *parent_b_lport =
            binding_lport_find(binding_lports, pb->virtual_parent);

        if (!parent_b_lport || !parent_b_lport->pb) {
            const struct sbrec_port_binding *parent_pb = lport_lookup_by_name(
                b_ctx_in->sbrec_port_binding_by_name, pb->virtual_parent);

            if (parent_pb && get_lport_type(parent_pb) == LP_VIF) {
                /* Its possible that the parent lport is not considered yet.
                 * So call consider_vif_lport() to process it first. */
                consider_vif_lport(parent_pb, b_ctx_in, b_ctx_out,
                                   parent_lbinding);
            }
        }

        parent_b_lport = local_binding_get_primary_lport(parent_lbinding);
        if (is_binding_lport_this_chassis(parent_b_lport,
                                          b_ctx_in->chassis_rec)) {
            virtual_b_lport =
                local_binding_add_lport(binding_lports, parent_lbinding, pb,
                                        LP_VIRTUAL);
        }
    }

    if (!consider_vif_lport_(pb, CAN_BIND_AS_MAIN, b_ctx_in, b_ctx_out,
                             virtual_b_lport)) {
        return false;
    }

    /* If the virtual lport is not bound to this chassis, then remove
     * its entry from the local_lport_ids if present.  This is required
     * when a virtual port moves from one chassis to other.*/
    if (!virtual_b_lport) {
        remove_related_lport(pb, b_ctx_out);
    }

    return true;
}

static bool
consider_localport(const struct sbrec_port_binding *pb,
                   struct binding_ctx_in *b_ctx_in,
                   struct binding_ctx_out *b_ctx_out)
{
    struct shash *local_bindings = &b_ctx_out->lbinding_data->bindings;
    struct local_binding *lbinding = local_binding_find(local_bindings,
                                                        pb->logical_port);
    /* Make sure there is no previous postponed port claim */
    sset_find_and_delete(b_ctx_out->postponed_ports, pb->logical_port);
    if (!lbinding) {
        return true;
    }

    local_binding_add_lport(&b_ctx_out->lbinding_data->lports, lbinding, pb,
                            LP_LOCALPORT);

    /* If the port binding is claimed, then release it as localport is claimed
     * by any ovn-controller. */
    enum can_bind can_bind = lport_can_bind_on_this_chassis(
        b_ctx_in->chassis_rec, pb);
    if (can_bind == CAN_BIND_AS_MAIN) {
        if (!release_lport_main_chassis(pb, !b_ctx_in->ovnsb_idl_txn,
            b_ctx_out->if_mgr)) {
            return false;
        }
    } else if (can_bind == CAN_BIND_AS_ADDITIONAL) {
        if (!release_lport_additional_chassis(pb, b_ctx_in->chassis_rec,
                                              !b_ctx_in->ovnsb_idl_txn)) {
            return false;
        }
    }

    if (can_bind) {
        remove_related_lport(pb, b_ctx_out);
    }

    /* Add all localnet ports to local_ifaces so that we allocate ct zones
     * for them. */
    update_local_lports(pb->logical_port, b_ctx_out, LPORT_STATUS_BOUND);

    update_related_lport(pb, b_ctx_out);
    return true;
}

/* Considers either claiming the lport or releasing the lport
 * for non VIF lports.
 */
static bool
consider_nonvif_lport_(const struct sbrec_port_binding *pb,
                       bool our_chassis, bool is_ha_chassis,
                       struct binding_ctx_in *b_ctx_in,
                       struct binding_ctx_out *b_ctx_out)
{
    struct local_datapath *ld =
        get_local_datapath(b_ctx_out->local_datapaths,
                           pb->datapath->tunnel_key);

    if (our_chassis) {
        update_local_lports(pb->logical_port, b_ctx_out, LPORT_STATUS_BOUND);
        if (!ld) {
            add_local_datapath(b_ctx_in->sbrec_datapath_binding_by_key,
                               b_ctx_in->sbrec_port_binding_by_datapath,
                               b_ctx_in->sbrec_port_binding_by_name,
                               pb->datapath, b_ctx_in->chassis_rec,
                               b_ctx_out->local_datapaths,
                               b_ctx_out->tracked_dp_bindings);
        } else {
            /* Add the peer datapath to the local datapaths if it's
             * not present yet.
             */
            const struct sbrec_port_binding *peer =
                lport_get_peer(pb, b_ctx_in->sbrec_port_binding_by_name);

            if (peer && need_add_peer_to_local(
                    b_ctx_in->sbrec_port_binding_by_name, pb,
                    peer, b_ctx_in->chassis_rec)) {
                add_local_datapath_peer_port(
                    pb, peer, b_ctx_in->chassis_rec,
                    b_ctx_in->sbrec_datapath_binding_by_key,
                    b_ctx_in->sbrec_port_binding_by_datapath,
                    b_ctx_in->sbrec_port_binding_by_name,
                    ld, b_ctx_out->local_datapaths,
                    b_ctx_out->tracked_dp_bindings);
            }
        }

        update_related_lport(pb, b_ctx_out);
        enum can_bind can_bind = lport_can_bind_on_this_chassis(
                                     b_ctx_in->chassis_rec, pb);
        return claim_lport(pb, NULL, b_ctx_in->chassis_rec, NULL,
                           !b_ctx_in->ovnsb_idl_txn, false,
                           b_ctx_out->tracked_dp_bindings,
                           b_ctx_out->if_mgr,
                           b_ctx_out->postponed_ports, can_bind);
    }
    if (!is_ha_chassis) {
        remove_related_lport(pb, b_ctx_out);
    }

    /* If port was postponed to now, and not our chassis, remove it from
     * postponed ports as it should not be claimed anymore.*/
    sset_find_and_delete(b_ctx_out->postponed_ports, pb->logical_port);

    if (pb->chassis == b_ctx_in->chassis_rec
            || is_additional_chassis(pb, b_ctx_in->chassis_rec)
            || if_status_is_port_claimed(b_ctx_out->if_mgr,
                                         pb->logical_port)) {
        if (!release_lport(pb, b_ctx_in->chassis_rec,
                          !b_ctx_in->ovnsb_idl_txn,
                          b_ctx_out->tracked_dp_bindings,
                          b_ctx_out->if_mgr)) {
            return false;
        }

        if (ld) {
            remove_local_datapath_peer_port(pb, ld,
                                            b_ctx_out->local_datapaths);
        }
    }

    return true;
}

static bool
consider_l2gw_lport(const struct sbrec_port_binding *pb,
                    struct binding_ctx_in *b_ctx_in,
                    struct binding_ctx_out *b_ctx_out)
{
    const char *chassis_id = smap_get(&pb->options, "l2gateway-chassis");
    bool our_chassis = chassis_id && !strcmp(chassis_id,
                                             b_ctx_in->chassis_rec->name);

    return consider_nonvif_lport_(pb, our_chassis, false, b_ctx_in, b_ctx_out);
}

static bool
consider_l3gw_lport(const struct sbrec_port_binding *pb,
                    struct binding_ctx_in *b_ctx_in,
                    struct binding_ctx_out *b_ctx_out)
{
    const char *chassis_id = smap_get(&pb->options, "l3gateway-chassis");
    bool our_chassis = chassis_id && !strcmp(chassis_id,
                                             b_ctx_in->chassis_rec->name);

    return consider_nonvif_lport_(pb, our_chassis, false, b_ctx_in, b_ctx_out);
}

static void
consider_localnet_lport(const struct sbrec_port_binding *pb,
                        struct binding_ctx_out *b_ctx_out)
{
    struct local_datapath *ld
        = get_local_datapath(b_ctx_out->local_datapaths,
                             pb->datapath->tunnel_key);
    if (!ld) {
        return;
    }

    bool pb_localnet_learn_fdb = smap_get_bool(&pb->options,
                                               "localnet_learn_fdb", false);
    if (pb_localnet_learn_fdb != b_ctx_out->localnet_learn_fdb) {
        b_ctx_out->localnet_learn_fdb = pb_localnet_learn_fdb;
        if (b_ctx_out->tracked_dp_bindings) {
            b_ctx_out->localnet_learn_fdb_changed = true;
            tracked_datapath_lport_add(pb, TRACKED_RESOURCE_UPDATED,
                                       b_ctx_out->tracked_dp_bindings);
        }
    }

    /* Add all localnet ports to local_ifaces so that we allocate ct zones
     * for them. */
    update_local_lports(pb->logical_port, b_ctx_out, LPORT_STATUS_BOUND);

    add_or_del_qos_port(pb->logical_port, true);
    update_related_lport(pb, b_ctx_out);
}

static bool
consider_ha_lport(const struct sbrec_port_binding *pb,
                  struct binding_ctx_in *b_ctx_in,
                  struct binding_ctx_out *b_ctx_out)
{
    bool our_chassis = false;
    bool is_ha_chassis = ha_chassis_group_contains(pb->ha_chassis_group,
                                                   b_ctx_in->chassis_rec);
    our_chassis = is_ha_chassis &&
                  ha_chassis_group_is_active(pb->ha_chassis_group,
                                             b_ctx_in->active_tunnels,
                                             b_ctx_in->chassis_rec);

    if (is_ha_chassis && !our_chassis) {
        /* If the chassis_rec is part of ha_chassis_group associated with
         * the port_binding 'pb', we need to add to the local_datapath
         * in even if its not active.
         *
         * If the chassis is active, consider_nonvif_lport_() takes care
         * of adding the datapath of this 'pb' to local datapaths.
         * */
        add_local_datapath(b_ctx_in->sbrec_datapath_binding_by_key,
                           b_ctx_in->sbrec_port_binding_by_datapath,
                           b_ctx_in->sbrec_port_binding_by_name,
                           pb->datapath, b_ctx_in->chassis_rec,
                           b_ctx_out->local_datapaths,
                           b_ctx_out->tracked_dp_bindings);
        update_related_lport(pb, b_ctx_out);
    }

    return consider_nonvif_lport_(pb, our_chassis, is_ha_chassis, b_ctx_in,
                                  b_ctx_out);
}

static bool
consider_cr_lport(const struct sbrec_port_binding *pb,
                  struct binding_ctx_in *b_ctx_in,
                  struct binding_ctx_out *b_ctx_out)
{
    return consider_ha_lport(pb, b_ctx_in, b_ctx_out);
}

static bool
consider_external_lport(const struct sbrec_port_binding *pb,
                        struct binding_ctx_in *b_ctx_in,
                        struct binding_ctx_out *b_ctx_out)
{
    return consider_ha_lport(pb, b_ctx_in, b_ctx_out);
}

/*
 * Builds local_bindings from the OVS interfaces.
 */
static void
build_local_bindings(struct binding_ctx_in *b_ctx_in,
                     struct binding_ctx_out *b_ctx_out)
{
    int i;
    for (i = 0; i < b_ctx_in->br_int->n_ports; i++) {
        const struct ovsrec_port *port_rec = b_ctx_in->br_int->ports[i];
        const char *iface_id;
        int j;

        if (!strcmp(port_rec->name, b_ctx_in->br_int->name)) {
            continue;
        }

        struct shash *local_bindings =
            &b_ctx_out->lbinding_data->bindings;
        for (j = 0; j < port_rec->n_interfaces; j++) {
            const struct ovsrec_interface *iface_rec;

            iface_rec = port_rec->interfaces[j];
            iface_id = smap_get(&iface_rec->external_ids, "iface-id");
            int64_t ofport = iface_rec->n_ofport ? *iface_rec->ofport : 0;

            if (iface_id && ofport > 0) {
                struct local_binding *lbinding =
                    local_binding_find(local_bindings, iface_id);
                if (!lbinding) {
                    lbinding = local_binding_create(iface_id, iface_rec);
                    local_binding_add(local_bindings, lbinding);
                } else {
                    lbinding->multiple_bindings = true;
                    static struct vlog_rate_limit rl =
                        VLOG_RATE_LIMIT_INIT(1, 5);
                    VLOG_WARN_RL(
                        &rl,
                        "Invalid configuration: iface-id is configured on "
                        "interfaces : [%s] and [%s]. Ignoring the "
                        "configuration on interface [%s]",
                        lbinding->iface->name, iface_rec->name,
                        iface_rec->name);
                }

                update_local_lports(iface_id, b_ctx_out,
                                    LPORT_STATUS_NOT_BOUND);
                smap_replace(b_ctx_out->local_iface_ids, iface_rec->name,
                             iface_id);
            } else if (smap_get_bool(&iface_rec->external_ids,
                       OVN_INSTALLED_EXT_ID, false)) {
                /* Interface should not be claimed (ovn_installed).
                 * This can happen if iface-id was removed as we recompute.
                 */
                if_status_mgr_remove_ovn_installed(b_ctx_out->if_mgr,
                                                   iface_rec);
            }
        }
    }
}

static bool consider_patch_port_for_local_datapaths(
        const struct sbrec_port_binding *,
        struct binding_ctx_in *, struct binding_ctx_out *);

void
binding_run(struct binding_ctx_in *b_ctx_in, struct binding_ctx_out *b_ctx_out)
{
    if (!b_ctx_in->chassis_rec) {
        return;
    }

    shash_clear_free_data(&_qos_ports);
    struct shash bridge_mappings = SHASH_INITIALIZER(&bridge_mappings);

    if (b_ctx_in->br_int) {
        build_local_bindings(b_ctx_in, b_ctx_out);
    }

    struct vector localnet_lports =
        VECTOR_EMPTY_INITIALIZER(struct sbrec_port_binding *);
    struct vector external_lports =
        VECTOR_EMPTY_INITIALIZER(struct sbrec_port_binding *);
    struct vector multichassis_ports =
        VECTOR_EMPTY_INITIALIZER(struct sbrec_port_binding *);
    struct vector vtep_lports =
        VECTOR_EMPTY_INITIALIZER(struct sbrec_port_binding *);

    /* Run through each binding record to see if it is resident on this
     * chassis and update the binding accordingly.  This includes both
     * directly connected logical ports and children of those ports
     * (which also includes virtual ports).
     */
    const struct sbrec_port_binding *pb;
    SBREC_PORT_BINDING_TABLE_FOR_EACH (pb,
                                       b_ctx_in->port_binding_table) {
        update_active_pb_ras_pd(pb, b_ctx_out->local_active_ports_ipv6_pd,
                                "ipv6_prefix_delegation");
        update_active_pb_ras_pd(pb, b_ctx_out->local_active_ports_ras,
                                "ipv6_ra_send_periodic");

        enum en_lport_type lport_type = get_lport_type(pb);

        switch (lport_type) {
        case LP_PATCH:
            update_related_lport(pb, b_ctx_out);
            consider_patch_port_for_local_datapaths(pb, b_ctx_in, b_ctx_out);
            break;

        case LP_VTEP:
            update_related_lport(pb, b_ctx_out);
            vector_push(&vtep_lports, &pb);
            break;

        case LP_LOCALPORT:
            consider_localport(pb, b_ctx_in, b_ctx_out);
            break;

        case LP_VIF:
            consider_vif_lport(pb, b_ctx_in, b_ctx_out, NULL);
            if (pb->additional_chassis) {
                vector_push(&multichassis_ports, &pb);
            }
            break;

        case LP_CONTAINER:
            consider_container_lport(pb, LP_CONTAINER, b_ctx_in, b_ctx_out);
            break;

        case LP_MIRROR:
            consider_container_lport(pb, LP_MIRROR, b_ctx_in, b_ctx_out);
            break;

        case LP_VIRTUAL:
            consider_virtual_lport(pb, b_ctx_in, b_ctx_out);
            break;

        case LP_L2GATEWAY:
            consider_l2gw_lport(pb, b_ctx_in, b_ctx_out);
            break;

        case LP_L3GATEWAY:
            consider_l3gw_lport(pb, b_ctx_in, b_ctx_out);
            break;

        case LP_CHASSISREDIRECT:
            consider_cr_lport(pb, b_ctx_in, b_ctx_out);
            break;

        case LP_EXTERNAL:
            consider_external_lport(pb, b_ctx_in, b_ctx_out);
            vector_push(&external_lports, &pb);
            break;

        case LP_LOCALNET:
            vector_push(&localnet_lports, &pb);
            break;

        case LP_REMOTE:
            /* Remote ports are related, since we can have rules in the
             * egress pipeline matching on traffic coming from another AZ. */
            update_related_lport(pb, b_ctx_out);
            break;

        case LP_UNKNOWN: {
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
            VLOG_WARN_RL(&rl,
                         "Unknown port binding type [%s] for port binding "
                         "[%s]. Does the ovn-controller need an update ?",
                         pb->type, pb->logical_port);
            break;
        }
        }
    }

    add_ovs_bridge_mappings(b_ctx_in->ovs_table, b_ctx_in->bridge_table,
                            &bridge_mappings);

    /* Run through each localnet lport list to see if it is a localnet port
     * on local datapaths discovered from above loop, and handles it
     * accordingly. */
    VECTOR_FOR_EACH (&localnet_lports, pb) {
        consider_localnet_lport(pb, b_ctx_out);
        update_ld_localnet_port(pb, &bridge_mappings,
                                b_ctx_out->local_datapaths);
    }
    vector_destroy(&localnet_lports);

    /* Run through external lport list to see if there are external ports
     * on local datapaths discovered from above loop, and update the
     * corresponding local datapath accordingly. */
    VECTOR_FOR_EACH (&external_lports, pb) {
        update_ld_external_ports(pb, b_ctx_out->local_datapaths);
    }
    vector_destroy(&external_lports);

    /* Run through multichassis lport list to see if there are ports
     * on local datapaths discovered from above loop, and update the
     * corresponding local datapath accordingly. */
    VECTOR_FOR_EACH (&multichassis_ports, pb) {
        update_ld_multichassis_ports(pb, b_ctx_out->local_datapaths);
    }
    vector_destroy(&multichassis_ports);

    /* Run through vtep lport list to see if there are vtep ports
     * on local datapaths discovered from above loop, and update the
     * corresponding local datapath accordingly. */
    VECTOR_FOR_EACH (&vtep_lports, pb) {
        update_ld_vtep_port(pb, b_ctx_out->local_datapaths);
    }
    vector_destroy(&vtep_lports);

    shash_destroy(&bridge_mappings);
    /* Remove stale QoS entries. */
    ovs_qos_entries_gc(b_ctx_in->ovs_idl_txn, b_ctx_in->ovsrec_port_by_qos,
                       b_ctx_in->ovsrec_queue_by_external_ids,
                       b_ctx_in->qos_table, b_ctx_out->qos_map);

    cleanup_claimed_port_timestamps();
}

/* Returns true if the database is all cleaned up, false if more work is
 * required. */
bool
binding_cleanup(struct ovsdb_idl_txn *ovnsb_idl_txn,
                const struct sbrec_port_binding_table *port_binding_table,
                const struct sbrec_chassis *chassis_rec)
{
    if (!ovnsb_idl_txn) {
        return false;
    }
    if (!chassis_rec) {
        return true;
    }

    const struct sbrec_port_binding *binding_rec;
    bool any_changes = false;
    SBREC_PORT_BINDING_TABLE_FOR_EACH (binding_rec, port_binding_table) {
        if (binding_rec->chassis == chassis_rec) {
            if (binding_rec->encap) {
                sbrec_port_binding_set_encap(binding_rec, NULL);
            }
            sbrec_port_binding_set_chassis(binding_rec, NULL);
            any_changes = true;
        }
        if (is_additional_chassis(binding_rec, chassis_rec)) {
            remove_additional_chassis(binding_rec, chassis_rec);
            any_changes = true;
        }
    }

    if (any_changes) {
        ovsdb_idl_txn_add_comment(
            ovnsb_idl_txn,
            "ovn-controller: removing all port bindings for '%s'",
            chassis_rec->name);
    }

    return !any_changes;
}

static void
remove_pb_from_local_datapath(const struct sbrec_port_binding *pb,
                              struct binding_ctx_out *b_ctx_out,
                              struct local_datapath *ld)
{
    remove_related_lport(pb, b_ctx_out);
    if (!strcmp(pb->type, "patch") ||
        !strcmp(pb->type, "l3gateway")) {
        remove_local_datapath_peer_port(pb, ld, b_ctx_out->local_datapaths);
    } else if (!strcmp(pb->type, "localnet")) {
        if (ld->localnet_port && !strcmp(ld->localnet_port->logical_port,
                                         pb->logical_port)) {
            ld->localnet_port = NULL;
        }
    } else if (!strcmp(pb->type, "external")) {
        remove_local_datapath_external_port(ld, pb->logical_port);
    } else if (!strcmp(pb->type, "vtep")) {
        if (ld->vtep_port && !strcmp(ld->vtep_port->logical_port,
                                     pb->logical_port)) {
            ld->vtep_port = NULL;
        }
    }
    remove_local_datapath_multichassis_port(ld, pb->logical_port);
}

static void
update_lport_tracking(const struct sbrec_port_binding *pb,
                      struct hmap *tracked_dp_bindings,
                      bool claimed)
{
    if (!tracked_dp_bindings) {
        return;
    }

    tracked_datapath_lport_add(
        pb, claimed ? TRACKED_RESOURCE_NEW : TRACKED_RESOURCE_REMOVED,
        tracked_dp_bindings);
}

/* Considers the ovs iface 'iface_rec' for claiming.
 * This function should be called if the external_ids:iface-id
 * and 'ofport' are set for the 'iface_rec'.
 *
 * If the local_binding for this 'iface_rec' already exists and its
 * already claimed, then this function will be no-op.
 */
static bool
consider_iface_claim(const struct ovsrec_interface *iface_rec,
                     const char *iface_id,
                     struct binding_ctx_in *b_ctx_in,
                     struct binding_ctx_out *b_ctx_out)
{
    smap_replace(b_ctx_out->local_iface_ids, iface_rec->name, iface_id);

    struct shash *local_bindings = &b_ctx_out->lbinding_data->bindings;
    struct local_binding *lbinding = local_binding_find(local_bindings,
                                                        iface_id);

    if (!lbinding) {
        lbinding = local_binding_create(iface_id, iface_rec);
        local_binding_add(local_bindings, lbinding);
    } else {
        if (lbinding->iface && lbinding->iface != iface_rec) {
            lbinding->multiple_bindings = true;
            b_ctx_out->local_lports_changed = true;
        }
        lbinding->iface = iface_rec;
    }

    struct binding_lport *b_lport =
        local_binding_get_primary_or_localport_lport(lbinding);
    const struct sbrec_port_binding *pb = NULL;
    if (!b_lport) {
        pb = lport_lookup_by_name(b_ctx_in->sbrec_port_binding_by_name,
                                  lbinding->name);
    } else {
        pb = b_lport->pb;
    }

    update_local_lports(iface_id, b_ctx_out, pb ? LPORT_STATUS_BOUND :
                        LPORT_STATUS_NOT_BOUND);
    if (!pb) {
        /* There is no port_binding row for this local binding. */
        return true;
    }

    /* Check if iface-id-ver just becomes correct */
    struct smap *external_ids_old =
        shash_find_data(b_ctx_in->iface_table_external_ids_old,
                        iface_rec->name);

    if (external_ids_old &&
        is_ext_id_changed(&iface_rec->external_ids,
                          external_ids_old,
                          "iface-id-ver")) {
            b_ctx_out->local_lports_changed = true;
    }

    /* If multiple bindings to the same port, remove the "old" binding.
     * This ensures that change tracking is correct.
     */
    if (lbinding->multiple_bindings) {
        remove_related_lport(pb, b_ctx_out);
    }

    enum en_lport_type lport_type = get_lport_type(pb);
    if (lport_type == LP_LOCALPORT) {
        return consider_localport(pb, b_ctx_in, b_ctx_out);
    }

    if (lport_type == LP_VIF &&
        !consider_vif_lport(pb, b_ctx_in, b_ctx_out, lbinding)) {
        return false;
    }

    /* Get the (updated) b_lport again for the lbinding. */
    b_lport = local_binding_get_primary_lport(lbinding);

    /*
     * Update the tracked_dp_bindings whenever an ofport
     * on a specific ovs port changes.
     * This update will trigger flow recomputation during
     * the incremental processing run which updates the local
     * flows in_port filed.
     */
    if (b_lport && ovsrec_interface_is_updated(iface_rec,
                                    OVSREC_INTERFACE_COL_OFPORT)) {
        tracked_datapath_lport_add(b_lport->pb, TRACKED_RESOURCE_UPDATED,
                                   b_ctx_out->tracked_dp_bindings);
        b_ctx_out->local_lports_changed = true;
    }


    /* Update the child local_binding's iface (if any children) and try to
     *  claim the container lbindings. */
    LIST_FOR_EACH (b_lport, list_node, &lbinding->binding_lports) {
        if (b_lport->type == LP_CONTAINER || b_lport->type == LP_MIRROR) {
            if (!consider_container_lport(b_lport->pb, b_lport->type,
                                          b_ctx_in, b_ctx_out)) {
                return false;
            }
        }
    }

    return true;
}

/* Considers the ovs interface 'iface_rec' for
 * releasing from this chassis if local_binding for this
 * 'iface_rec' (with 'iface_id' as key) already exists and
 * it is claimed by the chassis.
 *
 * The 'iface_id' could be cleared from the 'iface_rec'
 * and hence it is passed separately.
 *
 * This fuction should be called if
 *   - OVS interface 'iface_rec' is deleted.
 *   - OVS interface 'iface_rec' external_ids:iface-id is updated
 *     (with the old value being 'iface_id'.)
 *   - OVS interface ofport is reset to 0.
 * */
static bool
consider_iface_release(const struct ovsrec_interface *iface_rec,
                       const char *iface_id,
                       struct binding_ctx_in *b_ctx_in,
                       struct binding_ctx_out *b_ctx_out)
{
    struct local_binding *lbinding;
    struct shash *local_bindings = &b_ctx_out->lbinding_data->bindings;
    struct shash *binding_lports = &b_ctx_out->lbinding_data->lports;

    lbinding = local_binding_find(local_bindings, iface_id);

    if (lbinding) {
        if (lbinding->multiple_bindings) {
            VLOG_INFO("Multiple bindings for %s: force recompute to clean up",
                      iface_id);
            return false;
        } else {
            int64_t ofport = iface_rec->n_ofport ? *iface_rec->ofport : 0;
            if (lbinding->iface != iface_rec && !ofport) {
                /* If external_ids:iface-id is set within the same transaction
                 * as adding an interface to a bridge, ovn-controller is
                 * usually initially notified of ovs interface changes with
                 * ofport == 0. If the lport was bound to a different interface
                 * we do not want to release it.
                 */
                VLOG_DBG("Not releasing lport %s as %s was claimed "
                         "and %s was never bound)", iface_id, lbinding->iface ?
                         lbinding->iface->name : "", iface_rec->name);
                return true;
            }
        }

        struct binding_lport *b_lport =
            local_binding_get_primary_or_localport_lport(lbinding);

        if (is_binding_lport_this_chassis(b_lport, b_ctx_in->chassis_rec)) {
            struct local_datapath *ld =
                get_local_datapath(b_ctx_out->local_datapaths,
                                   b_lport->pb->datapath->tunnel_key);
            if (ld) {
                remove_pb_from_local_datapath(b_lport->pb,
                                              b_ctx_out, ld);
            }
            add_or_del_qos_port(b_lport->pb->logical_port, false);

            /* Release the primary binding lport and other children lports if
             * any. */
            LIST_FOR_EACH (b_lport, list_node, &lbinding->binding_lports) {
                if (!release_binding_lport(b_ctx_in->chassis_rec, b_lport,
                                           !b_ctx_in->ovnsb_idl_txn,
                                           b_ctx_out)) {
                    return false;
                }
            }
            if (lbinding->iface && lbinding->iface->name) {
                if_status_mgr_remove_ovn_installed(b_ctx_out->if_mgr,
                                           lbinding->iface);
            }

        } else if (b_lport && b_lport->type == LP_LOCALPORT) {
            /* lbinding is associated with a localport.  Remove it from the
             * related lports. */
            remove_related_lport(b_lport->pb, b_ctx_out);
        }

        /* Check if the lbinding has children of type PB_CONTAINER/PB_MIRROR.
         * If so, don't delete the local_binding. */
        if (!is_lbinding_container_parent(lbinding)) {
            local_binding_delete(lbinding, local_bindings, binding_lports,
                                 b_ctx_out->if_mgr);
        } else {
            /* Clear the iface of the local binding. */
            lbinding->iface = NULL;
        }
    }

    remove_local_lports(iface_id, b_ctx_out);
    smap_remove(b_ctx_out->local_iface_ids, iface_rec->name);

    return true;
}

static bool
is_iface_vif(const struct ovsrec_interface *iface_rec)
{
    if (iface_rec->type && iface_rec->type[0] &&
        strcmp(iface_rec->type, "internal")) {
        return false;
    }

    return true;
}

bool
is_iface_in_int_bridge(const struct ovsrec_interface *iface,
                       const struct ovsrec_bridge *br_int)
{
    for (size_t i = 0; i < br_int->n_ports; i++) {
        const struct ovsrec_port *p = br_int->ports[i];
        for (size_t j = 0; j < p->n_interfaces; j++) {
            if (!strcmp(iface->name, p->interfaces[j]->name)) {
                return true;
            }
        }
    }
    return false;
}

static bool
is_ext_id_changed(const struct smap *a, const struct smap *b, const char *key)
{
    const char *value_a = smap_get(a, key);
    const char *value_b = smap_get(b, key);
    if ((value_a && !value_b)
        || (!value_a && value_b)
        || (value_a && value_b && strcmp(value_a, value_b))) {
        return true;
    }
    return false;
}

/* Check if the change in 'iface_rec' is something we are interested in from
 * port binding perspective.  Return true if the change needs to be handled,
 * otherwise return false.
 *
 * The 'iface_rec' must be change tracked, i.e. iterator from
 * OVSREC_INTERFACE_TABLE_FOR_EACH_TRACKED. */
static bool
ovs_interface_change_need_handle(const struct ovsrec_interface *iface_rec,
                                 struct shash *iface_table_external_ids_old)
{
    if (ovsrec_interface_is_updated(iface_rec,
                                    OVSREC_INTERFACE_COL_NAME)) {
        return true;
    }
    if (ovsrec_interface_is_updated(iface_rec,
                                    OVSREC_INTERFACE_COL_OFPORT)) {
        return true;
    }
    if (ovsrec_interface_is_updated(iface_rec,
                                    OVSREC_INTERFACE_COL_TYPE)) {
        return true;
    }
    if (ovsrec_interface_is_updated(iface_rec,
                                    OVSREC_INTERFACE_COL_EXTERNAL_IDS)) {
        /* Compare the external_ids that we are interested in with the old
         * values:
         * - iface-id
         * - iface-id-ver
         * - encap-ip
         * For any other changes, such as ovn-installed, ovn-installed-ts, etc,
         * we don't need to handle. */
        struct smap *external_ids_old =
            shash_find_data(iface_table_external_ids_old, iface_rec->name);
        if (!external_ids_old) {
            return true;
        }
        if (is_ext_id_changed(&iface_rec->external_ids, external_ids_old,
                              "iface-id")) {
            return true;
        }
        if (is_ext_id_changed(&iface_rec->external_ids, external_ids_old,
                              "iface-id-ver")) {
            return true;
        }
        if (is_ext_id_changed(&iface_rec->external_ids, external_ids_old,
                              "encap-ip")) {
            return true;
        }
    }
    return false;
}

/* Returns true if the ovs interface changes were handled successfully,
 * false otherwise.
 */
bool
binding_handle_ovs_interface_changes(struct binding_ctx_in *b_ctx_in,
                                     struct binding_ctx_out *b_ctx_out)
{
    if (!b_ctx_in->chassis_rec) {
        return false;
    }

    bool handled = true;

    /* Run the tracked interfaces loop twice. One to handle deleted
     * changes. And another to handle add/update changes.
     * This will ensure correctness.
     *     *
     * We consider an OVS interface for release if one of the following
     * happens:
     *   1. OVS interface is deleted.
     *   2. external_ids:iface-id is cleared in which case we need to
     *      release the port binding corresponding to the previously set
     *      'old-iface-id' (which is stored in the smap
     *      'b_ctx_out->local_iface_ids').
     *   3. external_ids:iface-id is updated with a different value
     *      in which case we need to release the port binding corresponding
     *      to the previously set 'old-iface-id' (which is stored in the smap
     *      'b_ctx_out->local_iface_ids').
     *   4. ofport of the OVS interface is 0.
     *
     */
    const struct ovsrec_interface *iface_rec;
    OVSREC_INTERFACE_TABLE_FOR_EACH_TRACKED (iface_rec,
                                             b_ctx_in->iface_table) {
        const char *iface_id = smap_get(&iface_rec->external_ids, "iface-id");
        const char *old_iface_id = smap_get(b_ctx_out->local_iface_ids,
                                            iface_rec->name);
        if (!iface_id && !old_iface_id && !is_iface_vif(iface_rec)) {
            /* Right now we are not handling ovs_interface changes if the
             * interface doesn't have iface-id or didn't have it
             * previously. */
            handled = false;
            break;
        }

        const char *cleared_iface_id = NULL;
        if (!ovsrec_interface_is_deleted(iface_rec)) {
            int64_t ofport = iface_rec->n_ofport ? *iface_rec->ofport : 0;
            if (iface_id) {
                /* Check if iface_id is changed. If so we need to
                 * release the old port binding and associate this
                 * inteface to new port binding. */
                if (old_iface_id && strcmp(iface_id, old_iface_id)) {
                    cleared_iface_id = old_iface_id;
                } else if (ofport <= 0) {
                    /* If ofport is <= 0, we need to release the iface if
                     * already claimed. */
                    cleared_iface_id = iface_id;
                }
            } else if (old_iface_id) {
                cleared_iface_id = old_iface_id;
            }
        } else {
            cleared_iface_id = iface_id;
        }

        if (cleared_iface_id) {
            handled = consider_iface_release(iface_rec, cleared_iface_id,
                                             b_ctx_in, b_ctx_out);
        }

        if (!handled) {
            break;
        }
    }

    if (!handled) {
        /* This can happen if any non vif OVS interface is in the tracked
         * list or if consider_iface_release() returned false.
         * There is no need to process further. */
        return false;
    }

    /*
     * We consider an OVS interface for claiming if the following
     * 2 conditions are met:
     *   1. external_ids:iface-id is set.
     *   2. ofport of the OVS interface is > 0.
     *
     * So when an update of an OVS interface happens we see if these
     * conditions are still true. If so consider this interface for
     * claiming. This would be no-op if the update of the OVS interface
     * didn't change the above two fields.
     */
    OVSREC_INTERFACE_TABLE_FOR_EACH_TRACKED (iface_rec,
                                             b_ctx_in->iface_table) {
        /* Loop to handle create and update changes only. */
        if (ovsrec_interface_is_deleted(iface_rec)) {
            continue;
        }

        if (!ovs_interface_change_need_handle(
            iface_rec, b_ctx_in->iface_table_external_ids_old)) {
            continue;
        }

        const char *iface_id = smap_get(&iface_rec->external_ids, "iface-id");
        int64_t ofport = iface_rec->n_ofport ? *iface_rec->ofport : 0;
        if (iface_id && ofport > 0 &&
                is_iface_in_int_bridge(iface_rec, b_ctx_in->br_int)) {
            handled = consider_iface_claim(iface_rec, iface_id, b_ctx_in,
                                           b_ctx_out);
            if (!handled) {
                break;
            }
        }
    }

    return handled;
}

static void
handle_deleted_lport(const struct sbrec_port_binding *pb,
                     struct binding_ctx_in *b_ctx_in,
                     struct binding_ctx_out *b_ctx_out)
{
    /* If the binding is local, remove it. */
    struct local_datapath *ld =
        get_local_datapath(b_ctx_out->local_datapaths,
                           pb->datapath->tunnel_key);
    if (ld) {
        remove_pb_from_local_datapath(pb,
                                      b_ctx_out, ld);
        /* Only try to release the port if it was ever claimed.
         * If a port was added and deleted within the same ovn-controller loop,
         * it is seen as never claimed.
         */
        if (if_status_is_port_claimed(b_ctx_out->if_mgr, pb->logical_port)) {
            if_status_mgr_release_iface(b_ctx_out->if_mgr, pb->logical_port);
        }
        return;
    }

    /* Some ports, such a l3gw ports for non local datapaths, can be related
     * while not belonging to a local datapath. Remove such ports from
     * related_lports when they are deleted.
     */
    remove_related_lport(pb, b_ctx_out);

    /* If the binding is not local, if 'pb' is a L3 gateway port, we should
     * remove its peer, if that one is local.
     */
    pb = lport_get_l3gw_peer(pb, b_ctx_in->sbrec_port_binding_by_name);
    if (pb) {
        ld = get_local_datapath(b_ctx_out->local_datapaths,
                                pb->datapath->tunnel_key);
        if (ld) {
            remove_pb_from_local_datapath(pb, b_ctx_out,
                                          ld);
        }
        if (if_status_is_port_claimed(b_ctx_out->if_mgr, pb->logical_port)) {
            if_status_mgr_release_iface(b_ctx_out->if_mgr, pb->logical_port);
        }
    }
}

static bool
handle_deleted_vif_lport(const struct sbrec_port_binding *pb,
                         enum en_lport_type lport_type,
                         struct binding_ctx_in *b_ctx_in,
                         struct binding_ctx_out *b_ctx_out)
{
    struct local_binding *lbinding = NULL;

    struct shash *binding_lports = &b_ctx_out->lbinding_data->lports;
    struct binding_lport *b_lport = binding_lport_find(binding_lports, pb->logical_port);
    if (b_lport) {
        lbinding = b_lport->lbinding;

         /* Remove b_lport from local_binding. */
         binding_lport_delete(binding_lports, b_lport);
    }

    if (lbinding && lport_type == LP_VIF) {
        sset_find_and_delete(b_ctx_out->postponed_ports, pb->logical_port);
        /* We need to release the container/virtual binding lports (if any) if
         * deleted 'pb' type is LP_VIF. */
        struct binding_lport *c_lport;
        LIST_FOR_EACH (c_lport, list_node, &lbinding->binding_lports) {
            sset_find_and_delete(b_ctx_out->postponed_ports, c_lport->name);
            remove_local_lports(c_lport->pb->logical_port, b_ctx_out);
            remove_related_lport(c_lport->pb, b_ctx_out);
            if (!release_binding_lport(b_ctx_in->chassis_rec, c_lport,
                                       !b_ctx_in->ovnsb_idl_txn,
                                       b_ctx_out)) {
                return false;
            }
        }
    }

    /* If its a container or mirror lport, then delete its entry from
     * local_lports if present.
     * Note: If a normal lport is deleted, we don't want to remove
     * it from local_lports if there is a VIF entry.
     * consider_iface_release() takes care of removing from the local_lports
     * when the interface change happens. */
    if (lport_type == LP_CONTAINER || lport_type == LP_MIRROR) {
        remove_local_lports(pb->logical_port, b_ctx_out);
    }

    handle_deleted_lport(pb, b_ctx_in, b_ctx_out);
    if (lbinding && lbinding->iface && lbinding->iface->name) {
        if_status_mgr_remove_ovn_installed(b_ctx_out->if_mgr,
                                           lbinding->iface);
    }
    return true;
}

static bool
handle_updated_vif_lport(const struct sbrec_port_binding *pb,
                         enum en_lport_type lport_type,
                         struct binding_ctx_in *b_ctx_in,
                         struct binding_ctx_out *b_ctx_out)
{
    bool claimed = (pb->chassis == b_ctx_in->chassis_rec);
    bool handled = true;

    if (lport_type == LP_VIRTUAL) {
        handled = consider_virtual_lport(pb, b_ctx_in, b_ctx_out);
    } else if (lport_type == LP_CONTAINER ||
               lport_type == LP_MIRROR) {
        handled = consider_container_lport(pb, lport_type, b_ctx_in,
                                           b_ctx_out);
    } else {
        handled = consider_vif_lport(pb, b_ctx_in, b_ctx_out, NULL);
    }

    if (!handled) {
        return false;
    }

    bool now_claimed = (pb->chassis == b_ctx_in->chassis_rec);

    if (lport_type == LP_VIRTUAL || lport_type == LP_CONTAINER ||
        lport_type == LP_MIRROR || (claimed == now_claimed &&
        !is_additional_chassis(pb, b_ctx_in->chassis_rec))) {
        return true;
    }

    struct shash *local_bindings = &b_ctx_out->lbinding_data->bindings;
    struct local_binding *lbinding = local_binding_find(local_bindings,
                                                        pb->logical_port);

    /* If the ovs port backing this binding previously was removed in the
     * meantime, we won't have a local_binding for it.
     */
    if (!lbinding) {
        ovs_assert(!now_claimed);
        return true;
    }

    struct binding_lport *b_lport;
    LIST_FOR_EACH (b_lport, list_node, &lbinding->binding_lports) {
        if (b_lport->type == LP_CONTAINER || b_lport->type == LP_MIRROR) {
            handled = consider_container_lport(b_lport->pb, b_lport->type,
                                               b_ctx_in, b_ctx_out);
            if (!handled) {
                return false;
            }
        }
    }

    return true;
}

static bool
consider_patch_port_for_local_datapaths(const struct sbrec_port_binding *pb,
                                        struct binding_ctx_in *b_ctx_in,
                                        struct binding_ctx_out *b_ctx_out)
{
    const struct sbrec_port_binding *peer;
    struct local_datapath *peer_ld = NULL;
    struct local_datapath *ld = NULL;

    ld = get_local_datapath(b_ctx_out->local_datapaths,
                            pb->datapath->tunnel_key);

    peer = lport_get_peer(pb, b_ctx_in->sbrec_port_binding_by_name);
    if (!ld) {
        /* If 'ld' for this lport is not present, then check if
         * there is a peer for this lport. If peer is present
         * and peer's datapath is already in the local datapaths,
         * then add this lport's datapath to the local_datapaths.
         * */
        if (peer) {
            peer_ld = get_local_datapath(b_ctx_out->local_datapaths,
                                         peer->datapath->tunnel_key);
        }
        if (peer_ld && need_add_peer_to_local(
                b_ctx_in->sbrec_port_binding_by_name, peer,
                pb, b_ctx_in->chassis_rec)) {
            add_local_datapath(
                b_ctx_in->sbrec_datapath_binding_by_key,
                b_ctx_in->sbrec_port_binding_by_datapath,
                b_ctx_in->sbrec_port_binding_by_name,
                pb->datapath, b_ctx_in->chassis_rec,
                b_ctx_out->local_datapaths,
                b_ctx_out->tracked_dp_bindings);
        }
    } else {
        /* Add the peer datapath to the local datapaths if it's
         * not present yet.
         */
        if (peer && need_add_peer_to_local(
                b_ctx_in->sbrec_port_binding_by_name, pb,
                peer, b_ctx_in->chassis_rec)) {
            add_local_datapath_peer_port(
                pb, peer, b_ctx_in->chassis_rec,
                b_ctx_in->sbrec_datapath_binding_by_key,
                b_ctx_in->sbrec_port_binding_by_datapath,
                b_ctx_in->sbrec_port_binding_by_name,
                ld, b_ctx_out->local_datapaths,
                b_ctx_out->tracked_dp_bindings);
        }
    }

    /* If this chassis is requested - try to claim. */
    if (pb->requested_chassis == b_ctx_in->chassis_rec) {
        return claim_lport(pb, NULL, b_ctx_in->chassis_rec, NULL,
                           !b_ctx_in->ovnsb_idl_txn, false,
                           b_ctx_out->tracked_dp_bindings,
                           b_ctx_out->if_mgr,
                           b_ctx_out->postponed_ports, CAN_BIND_AS_MAIN);
    }
    /* If this chassis is claimed, but not requested to be; or requested for
     * some other chassis, but claimed by us - release. */
    if ((!pb->requested_chassis && !pb->n_additional_chassis && pb->chassis)
        || pb->chassis == b_ctx_in->chassis_rec
        || is_additional_chassis(pb, b_ctx_in->chassis_rec)
        || if_status_is_port_claimed(b_ctx_out->if_mgr, pb->logical_port)) {

        remove_local_lports(pb->logical_port, b_ctx_out);
        if (!release_lport(pb, b_ctx_in->chassis_rec,
                           !b_ctx_in->ovnsb_idl_txn,
                           b_ctx_out->tracked_dp_bindings,
                           b_ctx_out->if_mgr)) {
            return false;
        }
    }
    return true;
}

static bool
handle_updated_port(struct binding_ctx_in *b_ctx_in,
                    struct binding_ctx_out *b_ctx_out,
                    const struct sbrec_port_binding *pb)
{
    update_active_pb_ras_pd(pb, b_ctx_out->local_active_ports_ipv6_pd,
                            "ipv6_prefix_delegation");

    update_active_pb_ras_pd(pb, b_ctx_out->local_active_ports_ras,
                            "ipv6_ra_send_periodic");

    enum en_lport_type lport_type = get_lport_type(pb);

    struct binding_lport *b_lport =
        binding_lport_find(&b_ctx_out->lbinding_data->lports,
                           pb->logical_port);
    if (b_lport) {
        ovs_assert(b_lport->pb == pb);

        if (b_lport->type != lport_type) {
            b_lport->type = lport_type;
        }

        if (b_lport->lbinding) {
            if (!local_binding_handle_stale_binding_lports(
                    b_lport->lbinding, b_ctx_in, b_ctx_out)) {
                return false;
            }
        }
    }

    bool handled = true;

    switch (lport_type) {
    case LP_VIF:
    case LP_CONTAINER:
    case LP_MIRROR:
    case LP_VIRTUAL:
        /* If port binding type just changed, port might be a "related_lport"
         * while it should not. Remove it from that set. It will be added
         * back later if needed.
         */
        if (sbrec_port_binding_is_updated(pb, SBREC_PORT_BINDING_COL_TYPE)) {
            remove_related_lport(pb, b_ctx_out);
        }
        update_ld_multichassis_ports(pb, b_ctx_out->local_datapaths);
        handled = handle_updated_vif_lport(pb, lport_type, b_ctx_in,
                                           b_ctx_out);
        break;

    case LP_LOCALPORT:
        handled = consider_localport(pb, b_ctx_in, b_ctx_out);
        break;

    case LP_PATCH:
        update_related_lport(pb, b_ctx_out);
        handled = consider_patch_port_for_local_datapaths(pb, b_ctx_in,
                                                              b_ctx_out);
        break;

    case LP_VTEP:
        update_related_lport(pb, b_ctx_out);
        update_ld_vtep_port(pb, b_ctx_out->local_datapaths);
        /* VTEP lports are claimed/released by ovn-controller-vteps.
         * We are not sure what changed. */
        b_ctx_out->non_vif_ports_changed = true;
        break;

    case LP_L2GATEWAY:
        handled = consider_l2gw_lport(pb, b_ctx_in, b_ctx_out);
        break;

    case LP_L3GATEWAY:
        handled = consider_l3gw_lport(pb, b_ctx_in, b_ctx_out);
        break;

    case LP_CHASSISREDIRECT:
        handled = consider_cr_lport(pb, b_ctx_in, b_ctx_out);
        if (!handled) {
            break;
        }
        const char *distributed_port = smap_get(&pb->options,
                                                "distributed-port");
        if (!distributed_port) {
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
            VLOG_WARN_RL(&rl, "No distributed-port option set for "
                         "chassisredirect port %s", pb->logical_port);
            break;
        }
        const struct sbrec_port_binding *distributed_pb
            = lport_lookup_by_name(b_ctx_in->sbrec_port_binding_by_name,
                                   distributed_port);
        if (!distributed_pb) {
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
            VLOG_WARN_RL(&rl, "No port binding record for distributed "
                         "port %s referred by chassisredirect port %s",
                         distributed_port, pb->logical_port);
            break;
        }
        handled = consider_patch_port_for_local_datapaths(distributed_pb,
                                                          b_ctx_in,
                                                          b_ctx_out);
        break;

    case LP_EXTERNAL:
        handled = consider_external_lport(pb, b_ctx_in, b_ctx_out);
        update_ld_external_ports(pb, b_ctx_out->local_datapaths);
        break;

    case LP_LOCALNET: {
        consider_localnet_lport(pb, b_ctx_out);

        struct shash bridge_mappings =
            SHASH_INITIALIZER(&bridge_mappings);
        add_ovs_bridge_mappings(b_ctx_in->ovs_table,
                                b_ctx_in->bridge_table,
                                &bridge_mappings);
        update_ld_localnet_port(pb, &bridge_mappings,
                                b_ctx_out->local_datapaths);
        shash_destroy(&bridge_mappings);
        break;
    }

    case LP_REMOTE:
        update_related_lport(pb, b_ctx_out);
        break;

    case LP_UNKNOWN:
        break;
    }

    return handled;
}

/* Returns true if the port binding changes resulted in local binding
 * updates, false otherwise.
 */
bool
binding_handle_port_binding_changes(struct binding_ctx_in *b_ctx_in,
                                    struct binding_ctx_out *b_ctx_out)
{
    /* Run the tracked port binding loop twice to ensure correctness:
     * 1. First to handle deleted changes.  This is split in four sub-parts
     *    because child local bindings must be cleaned up first:
     *    a. Container ports first.
     *    b. Then virtual ports.
     *    c. Then regular VIFs.
     *    d. Last other ports.
     * 2. Second to handle add/update changes.
     */
    struct shash deleted_container_pbs =
        SHASH_INITIALIZER(&deleted_container_pbs);
    struct shash deleted_mirror_pbs =
        SHASH_INITIALIZER(&deleted_mirror_pbs);
    struct shash deleted_virtual_pbs =
        SHASH_INITIALIZER(&deleted_virtual_pbs);
    struct shash deleted_vif_pbs =
        SHASH_INITIALIZER(&deleted_vif_pbs);
    struct shash deleted_localport_pbs =
        SHASH_INITIALIZER(&deleted_localport_pbs);
    struct shash deleted_other_pbs =
        SHASH_INITIALIZER(&deleted_other_pbs);
    const struct sbrec_port_binding *pb;
    bool handled = true;

    SBREC_PORT_BINDING_TABLE_FOR_EACH_TRACKED (pb,
                                               b_ctx_in->port_binding_table) {
        if (!sbrec_port_binding_is_deleted(pb)) {
            continue;
        }

        delete_active_pb_ras_pd(pb, b_ctx_out->local_active_ports_ipv6_pd);
        delete_active_pb_ras_pd(pb, b_ctx_out->local_active_ports_ras);

        enum en_lport_type lport_type = get_lport_type(pb);

        struct binding_lport *b_lport =
            binding_lport_find(&b_ctx_out->lbinding_data->lports,
                               pb->logical_port);
        if (b_lport) {
            /* If the 'b_lport->type' and 'lport_type' don't match, then update
             * the b_lport->type to the updated 'lport_type'.  The function
             * binding_lport_check_and_cleanup() will cleanup the 'b_lport'
             * if required. */
            if (b_lport->type != lport_type) {
                b_lport->type = lport_type;
            }
            b_lport = binding_lport_check_and_cleanup(
                b_lport, &b_ctx_out->lbinding_data->lports);
        }

        if (lport_type == LP_VIF) {
            shash_add(&deleted_vif_pbs, pb->logical_port, pb);
        } else if (lport_type == LP_CONTAINER) {
            shash_add(&deleted_container_pbs, pb->logical_port, pb);
        } else if (lport_type == LP_MIRROR) {
            shash_add(&deleted_mirror_pbs, pb->logical_port, pb);
        } else if (lport_type == LP_VIRTUAL) {
            shash_add(&deleted_virtual_pbs, pb->logical_port, pb);
        } else if (lport_type == LP_LOCALPORT) {
            shash_add(&deleted_localport_pbs, pb->logical_port, pb);
        } else {
            shash_add(&deleted_other_pbs, pb->logical_port, pb);
        }

        add_or_del_qos_port(pb->logical_port, false);
    }

    struct shash_node *node;
    SHASH_FOR_EACH_SAFE (node, &deleted_container_pbs) {
        handled = handle_deleted_vif_lport(node->data, LP_CONTAINER, b_ctx_in,
                                           b_ctx_out);
        shash_delete(&deleted_container_pbs, node);
        if (!handled) {
            goto delete_done;
        }
    }

    SHASH_FOR_EACH_SAFE (node, &deleted_mirror_pbs) {
        handled = handle_deleted_vif_lport(node->data, LP_MIRROR, b_ctx_in,
                                           b_ctx_out);
        shash_delete(&deleted_mirror_pbs, node);
        if (!handled) {
            goto delete_done;
        }
    }

    SHASH_FOR_EACH_SAFE (node, &deleted_virtual_pbs) {
        handled = handle_deleted_vif_lport(node->data, LP_VIRTUAL, b_ctx_in,
                                           b_ctx_out);
        shash_delete(&deleted_virtual_pbs, node);
        if (!handled) {
            goto delete_done;
        }
    }

    SHASH_FOR_EACH_SAFE (node, &deleted_vif_pbs) {
        handled = handle_deleted_vif_lport(node->data, LP_VIF, b_ctx_in,
                                           b_ctx_out);
        shash_delete(&deleted_vif_pbs, node);
        if (!handled) {
            goto delete_done;
        }
    }

    SHASH_FOR_EACH_SAFE (node, &deleted_localport_pbs) {
        handle_deleted_vif_lport(node->data, LP_LOCALPORT, b_ctx_in,
                                 b_ctx_out);
        shash_delete(&deleted_localport_pbs, node);
    }

    SHASH_FOR_EACH_SAFE (node, &deleted_other_pbs) {
        handle_deleted_lport(node->data, b_ctx_in, b_ctx_out);
        shash_delete(&deleted_other_pbs, node);
    }

delete_done:
    shash_destroy(&deleted_container_pbs);
    shash_destroy(&deleted_mirror_pbs);
    shash_destroy(&deleted_virtual_pbs);
    shash_destroy(&deleted_vif_pbs);
    shash_destroy(&deleted_localport_pbs);
    shash_destroy(&deleted_other_pbs);

    if (!handled) {
        return false;
    }

    SBREC_PORT_BINDING_TABLE_FOR_EACH_TRACKED (pb,
                                               b_ctx_in->port_binding_table) {
        /* Loop to handle create and update changes only. */
        if (sbrec_port_binding_is_deleted(pb)) {
            continue;
        }

        if (sbrec_port_binding_is_updated(pb, SBREC_PORT_BINDING_COL_TYPE)) {
            update_ld_peers(pb, b_ctx_out->local_datapaths);
        }

        if (!handle_updated_port(b_ctx_in, b_ctx_out, pb)) {
            handled = false;
            break;
        }

        if (!sbrec_port_binding_is_new(pb) &&
            sbrec_port_binding_is_updated(pb,
                                          SBREC_PORT_BINDING_COL_TUNNEL_KEY) &&
            get_local_datapath(b_ctx_out->local_datapaths,
                               pb->datapath->tunnel_key)) {
            handled = false;
            break;
        }
    }

    /* Also handle any postponed (throttled) ports. */
    const char *port_name;
    struct sset postponed_ports = SSET_INITIALIZER(&postponed_ports);
    sset_clone(&postponed_ports, b_ctx_out->postponed_ports);
    SSET_FOR_EACH (port_name, &postponed_ports) {
        pb = lport_lookup_by_name(b_ctx_in->sbrec_port_binding_by_name,
                                  port_name);
        if (!pb) {
            sset_find_and_delete(b_ctx_out->postponed_ports, port_name);
            continue;
        }
        handled = handle_updated_port(b_ctx_in, b_ctx_out, pb);
        if (!handled) {
            break;
        }
    }
    sset_destroy(&postponed_ports);
    cleanup_claimed_port_timestamps();

    if (handled) {
        /* There may be new local datapaths added by the above handling, so go
         * through each port_binding of newly added local datapaths to update
         * related local_datapaths if needed. */
        struct shash bridge_mappings =
            SHASH_INITIALIZER(&bridge_mappings);
        add_ovs_bridge_mappings(b_ctx_in->ovs_table,
                                b_ctx_in->bridge_table,
                                &bridge_mappings);
        struct tracked_datapath *t_dp;
        HMAP_FOR_EACH (t_dp, node, b_ctx_out->tracked_dp_bindings) {
            if (t_dp->tracked_type != TRACKED_RESOURCE_NEW) {
                continue;
            }
            struct sbrec_port_binding *target =
                sbrec_port_binding_index_init_row(
                    b_ctx_in->sbrec_port_binding_by_datapath);
            sbrec_port_binding_index_set_datapath(target, t_dp->dp);

            SBREC_PORT_BINDING_FOR_EACH_EQUAL (pb, target,
                b_ctx_in->sbrec_port_binding_by_datapath) {
                enum en_lport_type lport_type = get_lport_type(pb);
                if (lport_type == LP_LOCALNET) {
                    consider_localnet_lport(pb, b_ctx_out);
                    update_ld_localnet_port(pb, &bridge_mappings,
                                            b_ctx_out->local_datapaths);
                } else if (lport_type == LP_EXTERNAL) {
                    update_ld_external_ports(pb, b_ctx_out->local_datapaths);
                } else if (lport_type == LP_VTEP) {
                    update_ld_vtep_port(pb, b_ctx_out->local_datapaths);
                } else if (pb->n_additional_chassis) {
                    update_ld_multichassis_ports(pb,
                                                 b_ctx_out->local_datapaths);
                }
            }
            sbrec_port_binding_index_destroy_row(target);
        }

        shash_destroy(&bridge_mappings);
    }

    return handled;
}

/* Static functions for local_lbindind and binding_lport. */
static struct local_binding *
local_binding_create(const char *name, const struct ovsrec_interface *iface)
{
    struct local_binding *lbinding = xzalloc(sizeof *lbinding);
    lbinding->name = xstrdup(name);
    lbinding->iface = iface;
    lbinding->multiple_bindings = false;
    ovs_list_init(&lbinding->binding_lports);

    return lbinding;
}

struct local_binding *
local_binding_find(const struct shash *local_bindings, const char *name)
{
    return shash_find_data(local_bindings, name);
}

static void
local_binding_add(struct shash *local_bindings, struct local_binding *lbinding)
{
    shash_add(local_bindings, lbinding->name, lbinding);
}

static void
local_binding_destroy(struct local_binding *lbinding,
                      struct shash *binding_lports)
{
    struct binding_lport *b_lport;
    LIST_FOR_EACH_POP (b_lport, list_node, &lbinding->binding_lports) {
        b_lport->lbinding = NULL;
        binding_lport_delete(binding_lports, b_lport);
    }

    free(lbinding->name);
    free(lbinding);
}

static void
local_binding_delete(struct local_binding *lbinding,
                     struct shash *local_bindings,
                     struct shash *binding_lports,
                     struct if_status_mgr *if_mgr)
{
    shash_find_and_delete(local_bindings, lbinding->name);
    if_status_mgr_delete_iface(if_mgr, lbinding->name, lbinding->iface);
    local_binding_destroy(lbinding, binding_lports);
}

static struct binding_lport *
local_binding_get_first_lport(struct local_binding *lbinding)
{
    if (!lbinding) {
        return NULL;
    }

    if (!ovs_list_is_empty(&lbinding->binding_lports)) {
        struct binding_lport *b_lport = NULL;
        b_lport = CONTAINER_OF(ovs_list_front(&lbinding->binding_lports),
                               struct binding_lport, list_node);

        return b_lport;
    }

    return NULL;
}

/* Returns the primary binding lport if present in lbinding's
 * binding lports list.  A binding lport is considered primary
 * if binding lport's type is LP_VIF and the name matches
 * with the 'lbinding'.
 */
static struct binding_lport *
local_binding_get_primary_lport(struct local_binding *lbinding)
{
    if (!lbinding) {
        return NULL;
    }

    struct binding_lport *b_lport = local_binding_get_first_lport(lbinding);
    if (b_lport && b_lport->type == LP_VIF &&
            !strcmp(lbinding->name, b_lport->name)) {
        return b_lport;
    }

    return NULL;
}

static struct binding_lport *
local_binding_get_primary_or_localport_lport(struct local_binding *lbinding)
{
    if (!lbinding) {
        return NULL;
    }

    struct binding_lport *b_lport = local_binding_get_first_lport(lbinding);
    if (b_lport && (b_lport->type == LP_VIF || b_lport->type == LP_LOCALPORT)
            && !strcmp(lbinding->name, b_lport->name)) {
        return b_lport;
    }

    return NULL;
}

static struct binding_lport *
local_binding_add_lport(struct shash *binding_lports,
                        struct local_binding *lbinding,
                        const struct sbrec_port_binding *pb,
                        enum en_lport_type b_type)
{
    struct binding_lport *b_lport =
        binding_lport_find(binding_lports, pb->logical_port);
    bool add_to_lport_list = false;
    if (!b_lport) {
        b_lport = binding_lport_create(pb, lbinding, b_type);
        binding_lport_add(binding_lports, b_lport);
        add_to_lport_list = true;
    } else if (b_lport->lbinding != lbinding) {
        add_to_lport_list = true;
        if (!ovs_list_is_empty(&b_lport->list_node)) {
            ovs_list_remove(&b_lport->list_node);
        }
        b_lport->lbinding = lbinding;
        b_lport->type = b_type;
    }

    if (add_to_lport_list) {
        if (b_type == LP_VIF) {
            ovs_list_push_front(&lbinding->binding_lports, &b_lport->list_node);
        } else {
            ovs_list_push_back(&lbinding->binding_lports, &b_lport->list_node);
        }
    }

    return b_lport;
}

/* This function handles the stale binding lports of 'lbinding' if 'lbinding'
 * doesn't have a primary binding lport.
 */
static bool
local_binding_handle_stale_binding_lports(struct local_binding *lbinding,
                                          struct binding_ctx_in *b_ctx_in,
                                          struct binding_ctx_out *b_ctx_out)
{
    /* Check if this lbinding has a primary binding_lport or
     * localport binding_lport or not. */
    struct binding_lport *p_lport =
        local_binding_get_primary_or_localport_lport(lbinding);
    if (p_lport) {
        /* Nothing to be done. */
        return true;
    }

    bool handled = true;
    struct binding_lport *b_lport;
    const struct sbrec_port_binding *pb;
    LIST_FOR_EACH_SAFE (b_lport, list_node, &lbinding->binding_lports) {
        /* Get the lport type again from the pb.  Its possible that the
         * pb type has changed. */
        enum en_lport_type pb_lport_type = get_lport_type(b_lport->pb);
        if (b_lport->type == LP_VIRTUAL && pb_lport_type == LP_VIRTUAL) {
            pb = b_lport->pb;
            binding_lport_delete(&b_ctx_out->lbinding_data->lports,
                                 b_lport);
            handled = consider_virtual_lport(pb, b_ctx_in, b_ctx_out);
        } else if ((b_lport->type == LP_CONTAINER &&
                   pb_lport_type == LP_CONTAINER) ||
                   (b_lport->type == LP_MIRROR &&
                   pb_lport_type == LP_MIRROR)) {
            /* For container lport, binding_lport is preserved so that when
             * the parent port is created, it can be considered.
             * consider_container_lport() creates the binding_lport for the parent
             * port (with iface set to NULL). */
            handled = consider_container_lport(b_lport->pb, b_lport->type,
                                               b_ctx_in, b_ctx_out);
        } else {
            /* This can happen when the lport type changes from one type
             * to another. Eg. from normal lport to external.  Release the
             * lport if it was claimed earlier and delete the b_lport. */
            handled = release_binding_lport(b_ctx_in->chassis_rec, b_lport,
                                            !b_ctx_in->ovnsb_idl_txn,
                                            b_ctx_out);
            binding_lport_delete(&b_ctx_out->lbinding_data->lports,
                                 b_lport);
        }

        if (!handled) {
            return false;
        }
    }

    return handled;
}

static struct binding_lport *
binding_lport_create(const struct sbrec_port_binding *pb,
                     struct local_binding *lbinding,
                     enum en_lport_type type)
{
    struct binding_lport *b_lport = xzalloc(sizeof *b_lport);
    b_lport->name = xstrdup(pb->logical_port);
    b_lport->pb = pb;
    b_lport->type = type;
    b_lport->lbinding = lbinding;
    ovs_list_init(&b_lport->list_node);

    return b_lport;
}

static void
binding_lport_add(struct shash *binding_lports, struct binding_lport *b_lport)
{
    shash_add(binding_lports, b_lport->pb->logical_port, b_lport);
}

static struct binding_lport *
binding_lport_find(struct shash *binding_lports, const char *lport_name)
{
    if (!lport_name) {
        return NULL;
    }

    return shash_find_data(binding_lports, lport_name);
}

static void
binding_lport_destroy(struct binding_lport *b_lport)
{
    if (!ovs_list_is_empty(&b_lport->list_node)) {
        ovs_list_remove(&b_lport->list_node);
    }

    binding_lport_clear_port_sec(b_lport);
    free(b_lport->name);
    free(b_lport);
}

static void
binding_lport_delete(struct shash *binding_lports,
                     struct binding_lport *b_lport)
{
    shash_find_and_delete(binding_lports, b_lport->name);
    binding_lport_destroy(b_lport);
}

void
port_binding_set_down(const struct sbrec_chassis *chassis_rec,
                      const struct sbrec_port_binding_table *pb_table,
                      const char *iface_id,
                      const struct uuid *pb_uuid)
{
        const struct sbrec_port_binding *pb =
            sbrec_port_binding_table_get_for_uuid(pb_table, pb_uuid);
        if (!pb) {
            VLOG_DBG("port_binding already deleted for %s", iface_id);
        } else if (pb->n_up && pb->up[0]) {
            bool up = false;
            sbrec_port_binding_set_up(pb, &up, 1);
            VLOG_INFO("Setting lport %s down in Southbound", pb->logical_port);
            set_pb_chassis_in_sbrec(pb, chassis_rec, false);
        }
}

void
port_binding_set_up(const struct sbrec_chassis *chassis_rec,
                    const struct sbrec_port_binding_table *pb_table,
                    const char *iface_id,
                    const struct uuid *pb_uuid)
{
    const struct sbrec_port_binding *pb =
        sbrec_port_binding_table_get_for_uuid(pb_table, pb_uuid);
    if (!pb) {
        VLOG_DBG("port_binding already deleted for %s", iface_id);
        return;
    }
    if (pb->n_up && !pb->up[0]) {
        bool up = true;
        sbrec_port_binding_set_up(pb, &up, 1);
        VLOG_INFO("Setting lport %s up in Southbound", pb->logical_port);
        set_pb_chassis_in_sbrec(pb, chassis_rec, true);
    }
}

void
port_binding_set_pb(const struct sbrec_chassis *chassis_rec,
                    const struct sbrec_port_binding_table *pb_table,
                    const char *iface_id,
                    const struct uuid *pb_uuid,
                    enum can_bind bind_type)
{
    const struct sbrec_port_binding *pb =
        sbrec_port_binding_table_get_for_uuid(pb_table, pb_uuid);
    if (!pb) {
        VLOG_DBG("port_binding already deleted for %s", iface_id);
        return;
    }
    if (bind_type == CAN_BIND_AS_MAIN) {
        set_pb_chassis_in_sbrec(pb, chassis_rec, true);
    } else  if (bind_type == CAN_BIND_AS_ADDITIONAL) {
        set_pb_additional_chassis_in_sbrec(pb, chassis_rec, true);
    }
}

bool
port_binding_pb_chassis_is_set(
    const struct sbrec_chassis *chassis_rec,
    const struct sbrec_port_binding_table *pb_table,
    const struct uuid *pb_uuid)
{
    const struct sbrec_port_binding *pb =
        sbrec_port_binding_table_get_for_uuid(pb_table, pb_uuid);

    if (pb && ((pb->chassis == chassis_rec) ||
        is_additional_chassis(pb, chassis_rec))) {
        return true;
    }
    return false;
}

bool
port_binding_is_up(const struct sbrec_chassis *chassis_rec,
                   const struct sbrec_port_binding_table *pb_table,
                   const struct uuid *pb_uuid)
{
    const struct sbrec_port_binding *pb =
        sbrec_port_binding_table_get_for_uuid(pb_table, pb_uuid);

    if (!pb || pb->chassis != chassis_rec) {
        return false;
    }

    if (pb->n_up && !pb->up[0]) {
        return false;
    }
    return true;
}

static void
binding_lport_set_up(struct binding_lport *b_lport, bool sb_readonly)
{
    if (sb_readonly || !b_lport || !b_lport->pb->n_up || b_lport->pb->up[0]) {
        return;
    }

    VLOG_INFO("Setting lport %s up in Southbound", b_lport->pb->logical_port);
    bool up = true;
    sbrec_port_binding_set_up(b_lport->pb, &up, 1);
}

static void
binding_lport_set_down(struct binding_lport *b_lport, bool sb_readonly)
{
    if (sb_readonly || !b_lport || !b_lport->pb->n_up || !b_lport->pb->up[0]) {
        return;
    }
    VLOG_INFO("Setting lport %s down in Southbound", b_lport->name);

    bool up = false;
    sbrec_port_binding_set_up(b_lport->pb, &up, 1);
}

static const struct sbrec_port_binding *
binding_lport_get_parent_pb(struct binding_lport *b_lport)
{
    if (!b_lport) {
        return NULL;
    }

    if (b_lport->type == LP_VIF) {
        return NULL;
    }

    struct local_binding *lbinding = b_lport->lbinding;
    ovs_assert(lbinding);

    struct binding_lport *parent_b_lport =
        local_binding_get_primary_lport(lbinding);

    return parent_b_lport ? parent_b_lport->pb : NULL;
}

/* This function checks and cleans up the 'b_lport' if it is
 * not in the correct state.
 *
 * If the 'b_lport' type is LP_VIF, then its name and its lbinding->name
 * should match.  Otherwise this should be cleaned up.
 *
 * If the 'b_lport' type is LP_CONTAINER or LP_MIRROR, then its parent_port
 * name should be the same as its lbinding's name. Otherwise this should
 * be cleaned up.
 *
 * If the 'b_lport' type is LP_VIRTUAL, then its virtual parent name
 * should be the same as its lbinding's name.  Otherwise this
 * should be cleaned up.
 *
 * If the 'b_lport' type is not LP_VIF, LP_CONTAINER or LP_VIRTUAL, it
 * should be cleaned up.  This can happen if the CMS changes
 * the port binding type.
 */
static struct binding_lport *
binding_lport_check_and_cleanup(struct binding_lport *b_lport,
                                struct shash *binding_lports)
{
    bool cleanup_blport = false;

    if (!b_lport->lbinding) {
        cleanup_blport = true;
        goto cleanup;
    }

    switch (b_lport->type) {
    case LP_VIF:
    case LP_LOCALPORT:
        if (strcmp(b_lport->name, b_lport->lbinding->name)) {
            cleanup_blport = true;
        }
        break;

    case LP_CONTAINER:
        if (strcmp(b_lport->pb->parent_port, b_lport->lbinding->name)) {
            cleanup_blport = true;
        }
        break;

    case LP_MIRROR:
        if (strcmp(b_lport->pb->mirror_port, b_lport->lbinding->name)) {
            cleanup_blport = true;
        }
        break;

    case LP_VIRTUAL:
        if (!b_lport->pb->virtual_parent ||
            strcmp(b_lport->pb->virtual_parent, b_lport->lbinding->name)) {
            cleanup_blport = true;
        }
        break;

    case LP_PATCH:
    case LP_VTEP:
    case LP_L2GATEWAY:
    case LP_L3GATEWAY:
    case LP_CHASSISREDIRECT:
    case LP_EXTERNAL:
    case LP_LOCALNET:
    case LP_REMOTE:
    case LP_UNKNOWN:
        cleanup_blport = true;
    }

cleanup:
    if (cleanup_blport) {
        binding_lport_delete(binding_lports, b_lport);
        return NULL;
    }

    return b_lport;
}


static bool
binding_lport_has_port_sec_changed(struct binding_lport *b_lport,
                                   const struct sbrec_port_binding *pb)
{
    if (b_lport->n_port_security != pb->n_port_security) {
        return true;
    }

    for (size_t i = 0; i < b_lport->n_port_security; i++) {
        if (strcmp(b_lport->port_security[i], pb->port_security[i])) {
            return true;
        }
    }

    return false;
}

static void
binding_lport_clear_port_sec(struct binding_lport *b_lport)
{
    for (size_t i = 0; i < b_lport->n_port_security; i++) {
        free(b_lport->port_security[i]);
    }
    free(b_lport->port_security);
    b_lport->n_port_security = 0;
}

static bool
binding_lport_update_port_sec(struct binding_lport *b_lport,
                              const struct sbrec_port_binding *pb)
{
    if (binding_lport_has_port_sec_changed(b_lport, pb)) {
        binding_lport_clear_port_sec(b_lport);
        b_lport->port_security =
            pb->n_port_security ?
            xmalloc(pb->n_port_security * sizeof *b_lport->port_security) :
            NULL;

        b_lport->n_port_security = pb->n_port_security;
        for (size_t i = 0; i < pb->n_port_security; i++) {
            b_lport->port_security[i] = xstrdup(pb->port_security[i]);
        }

        return true;
    }

    return false;
}

static bool
ovs_iface_matches_lport_iface_id_ver(const struct ovsrec_interface *iface,
                                     const struct sbrec_port_binding *pb)
{
    const char *pb_iface_id_ver = smap_get(&pb->options, "iface-id-ver");

    if (pb_iface_id_ver) {
        const char *iface_id_ver = smap_get(&iface->external_ids,
                                            "iface-id-ver");
        if (!iface_id_ver || strcmp(pb_iface_id_ver, iface_id_ver)) {
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
            VLOG_INFO_RL(&rl, "Mismatch iface-id-ver for lport %s, "
                         "expected %s, found %s", pb->logical_port,
                         pb_iface_id_ver,
                         iface_id_ver ? iface_id_ver : "<empty>");
            return false;
        }
    }

    return true;
}

void
binding_destroy(void)
{
    shash_destroy_free_data(&_claimed_ports);
    shash_destroy_free_data(&_qos_ports);
    sset_clear(&_postponed_ports);
}