summaryrefslogtreecommitdiffstats
path: root/pbrd/pbr_nht.c
blob: 4f7882fb22135aad1400d3ce447b063dcdef92f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * PBR-nht Code
 * Copyright (C) 2018 Cumulus Networks, Inc.
 *               Donald Sharp
 */
#include <zebra.h>

#include <log.h>
#include <nexthop.h>
#include "nexthop_group.h"
#include "nexthop_group_private.h"
#include <hash.h>
#include <jhash.h>
#include <vty.h>
#include <zclient.h>
#include <debug.h>

#include "pbrd/pbr_nht.h"
#include "pbrd/pbr_map.h"
#include "pbrd/pbr_zebra.h"
#include "pbrd/pbr_memory.h"
#include "pbrd/pbr_debug.h"

DEFINE_MTYPE_STATIC(PBRD, PBR_NHG, "PBR Nexthop Groups");

struct hash *pbr_nhg_hash;
static struct hash *pbr_nhrc_hash;
static struct hash *pbr_nhg_allocated_id_hash;

static uint32_t pbr_nhg_low_table;
static uint32_t pbr_nhg_high_table;
static uint32_t pbr_next_unallocated_table_id;
static uint32_t pbr_nhg_low_rule;
static uint32_t pbr_nhg_high_rule;

static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
					  struct nexthop_group nhg);
static void
pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
				struct nexthop_group nhg,
				enum nexthop_types_t nh_type);

/*
 * Nexthop refcount.
 */
struct nhrc {
	struct nexthop nexthop;
	unsigned int refcount;
};

/* Hash functions for pbr_nhrc_hash ---------------------------------------- */

static void *pbr_nhrc_hash_alloc(void *p)
{
	struct nhrc *nhrc = XCALLOC(MTYPE_PBR_NHG, sizeof(struct nhrc));
	nhrc->nexthop = *(struct nexthop *)p;
	nhrc->nexthop.next = NULL;
	nhrc->nexthop.prev = NULL;
	return nhrc;
}

static bool pbr_nhrc_hash_equal(const void *arg1, const void *arg2)
{
	const struct nexthop *nh1, *nh2;

	nh1 = arg1;
	nh2 = arg2;

	return nexthop_same(nh1, nh2);
}

/* ------------------------------------------------------------------------- */

static void *pbr_nh_alloc(void *p)
{
	struct pbr_nexthop_cache *new;
	struct pbr_nexthop_cache *pnhc = (struct pbr_nexthop_cache *)p;
	struct nhrc *nhrc;

	new = XCALLOC(MTYPE_PBR_NHG, sizeof(*new));
	nhrc = hash_get(pbr_nhrc_hash, &pnhc->nexthop, pbr_nhrc_hash_alloc);
	new->nexthop = nhrc->nexthop;

	/* Decremented again in pbr_nh_delete */
	++nhrc->refcount;

	DEBUGD(&pbr_dbg_nht, "%s: Sending nexthop to Zebra", __func__);

	pbr_send_rnh(&new->nexthop, true);

	new->valid = false;
	return new;
}

static void pbr_nh_delete(struct pbr_nexthop_cache **pnhc)
{
	struct nhrc *nhrc;

	nhrc = hash_lookup(pbr_nhrc_hash, &((*pnhc)->nexthop));

	if (nhrc)
		--nhrc->refcount;
	if (!nhrc || nhrc->refcount == 0) {
		DEBUGD(&pbr_dbg_nht, "%s: Removing nexthop from Zebra",
		       __func__);
		pbr_send_rnh(&((*pnhc)->nexthop), false);
	}
	if (nhrc && nhrc->refcount == 0) {
		hash_release(pbr_nhrc_hash, nhrc);
		XFREE(MTYPE_PBR_NHG, nhrc);
	}

	XFREE(MTYPE_PBR_NHG, *pnhc);
}

static void pbr_nh_delete_iterate(struct hash_bucket *b, void *p)
{
	pbr_nh_delete((struct pbr_nexthop_cache **)&b->data);
}

static uint32_t pbr_nh_hash_key(const void *arg)
{
	uint32_t key;
	const struct pbr_nexthop_cache *pbrnc = arg;

	key = nexthop_hash(&pbrnc->nexthop);

	return key;
}

static bool pbr_nh_hash_equal(const void *arg1, const void *arg2)
{
	const struct pbr_nexthop_cache *pbrnc1 =
		(const struct pbr_nexthop_cache *)arg1;
	const struct pbr_nexthop_cache *pbrnc2 =
		(const struct pbr_nexthop_cache *)arg2;

	if (pbrnc1->nexthop.vrf_id != pbrnc2->nexthop.vrf_id)
		return false;

	if (pbrnc1->nexthop.ifindex != pbrnc2->nexthop.ifindex)
		return false;

	if (pbrnc1->nexthop.type != pbrnc2->nexthop.type)
		return false;

	switch (pbrnc1->nexthop.type) {
	case NEXTHOP_TYPE_IFINDEX:
		return pbrnc1->nexthop.ifindex == pbrnc2->nexthop.ifindex;
	case NEXTHOP_TYPE_IPV4_IFINDEX:
	case NEXTHOP_TYPE_IPV4:
		return pbrnc1->nexthop.gate.ipv4.s_addr
		       == pbrnc2->nexthop.gate.ipv4.s_addr;
	case NEXTHOP_TYPE_IPV6_IFINDEX:
	case NEXTHOP_TYPE_IPV6:
		return !memcmp(&pbrnc1->nexthop.gate.ipv6,
			       &pbrnc2->nexthop.gate.ipv6, 16);
	case NEXTHOP_TYPE_BLACKHOLE:
		return pbrnc1->nexthop.bh_type == pbrnc2->nexthop.bh_type;
	}

	/*
	 * We should not get here
	 */
	return false;
}

static void pbr_nhgc_delete(struct pbr_nexthop_group_cache *p)
{
	hash_iterate(p->nhh, pbr_nh_delete_iterate, NULL);
	hash_free(p->nhh);
	XFREE(MTYPE_PBR_NHG, p);
}

static void *pbr_nhgc_alloc(void *p)
{
	struct pbr_nexthop_group_cache *new;
	struct pbr_nexthop_group_cache *pnhgc =
		(struct pbr_nexthop_group_cache *)p;

	new = XCALLOC(MTYPE_PBR_NHG, sizeof(*new));

	strlcpy(new->name, pnhgc->name, sizeof(pnhgc->name));
	pbr_nht_reserve_next_table_id(new);

	DEBUGD(&pbr_dbg_nht, "%s: NHT: %s assigned Table ID: %u", __func__,
	       new->name, new->table_id);

	new->nhh = hash_create_size(8, pbr_nh_hash_key, pbr_nh_hash_equal,
				    "PBR NH Cache Hash");
	return new;
}


void pbr_nhgroup_add_cb(const char *name)
{
	struct pbr_nexthop_group_cache *pnhgc;
	struct nexthop_group_cmd *nhgc;

	nhgc = nhgc_find(name);

	if (!nhgc) {
		DEBUGD(&pbr_dbg_nht, "%s: Could not find nhgc with name: %s",
		       __func__, name);
		return;
	}

	pnhgc = pbr_nht_add_group(name);

	if (!pnhgc)
		return;

	DEBUGD(&pbr_dbg_nht, "%s: Added nexthop-group %s", __func__, name);

	pbr_map_check_nh_group_change(name);
}

void pbr_nhgroup_modify_cb(const struct nexthop_group_cmd *nhgc)
{
}

void pbr_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd *nhgc,
				const struct nexthop *nhop)
{
	char debugstr[256];
	struct pbr_nexthop_group_cache pnhgc_find = {};
	struct pbr_nexthop_group_cache *pnhgc;
	struct pbr_nexthop_cache pnhc_find = {};
	struct pbr_nexthop_cache *pnhc;

	/* find pnhgc by name */
	strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name));
	pnhgc = hash_lookup(pbr_nhg_hash, &pnhgc_find);

	if (!pnhgc) {
		/* Check if configured table range is exhausted */
		if (!pbr_nht_has_unallocated_table()) {
			zlog_warn(
				"%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
				__func__, nhgc->name);
			return;
		}

		/* No nhgc but range not exhausted? Then alloc it */
		pnhgc = hash_get(pbr_nhg_hash, &pnhgc_find, pbr_nhgc_alloc);
	}

	/* create & insert new pnhc into pnhgc->nhh */
	pnhc_find.nexthop = *nhop;
	pnhc = hash_get(pnhgc->nhh, &pnhc_find, pbr_nh_alloc);

	/* set parent pnhgc */
	pnhc->parent = pnhgc;

	if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) {
		nexthop2str(nhop, debugstr, sizeof(debugstr));
		DEBUGD(&pbr_dbg_nht, "%s: Added %s to nexthop-group %s",
		       __func__, debugstr, nhgc->name);
	}

	pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg);
	pbr_map_check_nh_group_change(nhgc->name);

	if (nhop->type == NEXTHOP_TYPE_IFINDEX
	    || (nhop->type == NEXTHOP_TYPE_IPV6_IFINDEX
		&& IN6_IS_ADDR_LINKLOCAL(&nhop->gate.ipv6))) {
		struct interface *ifp;

		ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
		if (ifp)
			pbr_nht_nexthop_interface_update(ifp);
	}
}

void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd *nhgc,
				const struct nexthop *nhop)
{
	char debugstr[256];
	struct pbr_nexthop_group_cache pnhgc_find = {};
	struct pbr_nexthop_group_cache *pnhgc;
	struct pbr_nexthop_cache pnhc_find = {};
	struct pbr_nexthop_cache *pnhc;
	enum nexthop_types_t nh_type = nhop->type;

	/* find pnhgc by name */
	strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name));
	pnhgc = hash_lookup(pbr_nhg_hash, &pnhgc_find);

	/*
	 * Ignore deletions of nhg we did not / could not allocate nhgc for
	 * Occurs when PBR table range is full but new nhg keep coming in
	 */
	if (!pnhgc)
		return;

	/* delete pnhc from pnhgc->nhh */
	pnhc_find.nexthop = *nhop;
	pnhc = hash_release(pnhgc->nhh, &pnhc_find);

	/* delete pnhc */
	pbr_nh_delete(&pnhc);

	if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) {
		nexthop2str(nhop, debugstr, sizeof(debugstr));
		DEBUGD(&pbr_dbg_nht, "%s: Removed %s from nexthop-group %s",
		       __func__, debugstr, nhgc->name);
	}

	if (pnhgc->nhh->count)
		pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg);
	else
		pbr_nht_uninstall_nexthop_group(pnhgc, nhgc->nhg, nh_type);

	pbr_map_check_nh_group_change(nhgc->name);
}

void pbr_nhgroup_delete_cb(const char *name)
{
	DEBUGD(&pbr_dbg_nht, "%s: Removed nexthop-group %s", __func__, name);

	/* delete group from all pbrms's */
	pbr_nht_delete_group(name);

	pbr_map_check_nh_group_change(name);
}

static void
pbr_nht_find_nhg_from_table_update(struct pbr_nexthop_group_cache *pnhgc,
				   uint32_t table_id, bool installed)
{
	if (pnhgc->table_id == table_id) {
		DEBUGD(&pbr_dbg_nht, "%s: %s: Table ID (%u) matches %s",
		       __func__, (installed ? "install" : "remove"), table_id,
		       pnhgc->name);

		pnhgc->installed = installed;
		pnhgc->valid = installed;
		pbr_map_schedule_policy_from_nhg(pnhgc->name, pnhgc->installed);
	}
}

static void pbr_nht_find_nhg_from_table_install(struct hash_bucket *b,
						void *data)
{
	struct pbr_nexthop_group_cache *pnhgc =
		(struct pbr_nexthop_group_cache *)b->data;
	uint32_t table_id = *(uint32_t *)data;

	pbr_nht_find_nhg_from_table_update(pnhgc, table_id, true);
}

void pbr_nht_route_installed_for_table(uint32_t table_id)
{
	hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_install,
		     &table_id);
}

static void pbr_nht_find_nhg_from_table_remove(struct hash_bucket *b,
					       void *data)
{
	struct pbr_nexthop_group_cache *pnhgc =
		(struct pbr_nexthop_group_cache *)b->data;
	uint32_t table_id = *(uint32_t *)data;

	pbr_nht_find_nhg_from_table_update(pnhgc, table_id, false);
}

void pbr_nht_route_removed_for_table(uint32_t table_id)
{
	hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_remove,
		     &table_id);
}

/*
 * Loop through all nexthops in a nexthop group to check that they are all the
 * same. If they are not all the same, log this peculiarity.
 *
 * nhg
 *    The nexthop group to check
 *
 * Returns:
 *    - AFI of last nexthop in the group
 *    - AFI_MAX on error
 */
static afi_t pbr_nht_which_afi(struct nexthop_group nhg,
			       enum nexthop_types_t nh_type)
{
	struct nexthop *nexthop;
	afi_t install_afi = AFI_MAX;
	bool v6, v4, bh;

	if (nh_type) {
		switch (nh_type) {
		case NEXTHOP_TYPE_IPV4:
		case NEXTHOP_TYPE_IPV4_IFINDEX:
			return AFI_IP;
		case NEXTHOP_TYPE_IPV6:
		case NEXTHOP_TYPE_IPV6_IFINDEX:
			return AFI_IP6;
		case NEXTHOP_TYPE_IFINDEX:
		case NEXTHOP_TYPE_BLACKHOLE:
			return AFI_MAX;
		}
	}

	v6 = v4 = bh = false;

	for (ALL_NEXTHOPS(nhg, nexthop)) {
		nh_type = nexthop->type;

		switch (nh_type) {
		case NEXTHOP_TYPE_IFINDEX:
			break;
		case NEXTHOP_TYPE_IPV4:
		case NEXTHOP_TYPE_IPV4_IFINDEX:
			v6 = true;
			install_afi = AFI_IP;
			break;
		case NEXTHOP_TYPE_IPV6:
		case NEXTHOP_TYPE_IPV6_IFINDEX:
			v4 = true;
			install_afi = AFI_IP6;
			break;
		case NEXTHOP_TYPE_BLACKHOLE:
			bh = true;
			break;
		}
	}

	/* Interface and/or blackhole nexthops only. */
	if (!v4 && !v6)
		install_afi = AFI_MAX;

	if (!bh && v6 && v4)
		DEBUGD(&pbr_dbg_nht,
		       "%s: Saw both V6 and V4 nexthops...using %s", __func__,
		       afi2str(install_afi));
	if (bh && (v6 || v4))
		DEBUGD(&pbr_dbg_nht,
		       "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.",
		       __func__, v4 ? "v4" : "", (v4 && v6) ? " and " : "",
		       v6 ? "v6" : "");

	return install_afi;
}

static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
					  struct nexthop_group nhg)
{
	afi_t install_afi;
	enum nexthop_types_t nh_type = 0;

	install_afi = pbr_nht_which_afi(nhg, nh_type);

	route_add(pnhgc, nhg, install_afi);
}

static void
pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
				struct nexthop_group nhg,
				enum nexthop_types_t nh_type)
{
	afi_t install_afi;

	install_afi = pbr_nht_which_afi(nhg, nh_type);

	pnhgc->installed = false;
	pnhgc->valid = false;
	route_delete(pnhgc, install_afi);
}

void pbr_nht_change_group(const char *name)
{
	struct nexthop_group_cmd *nhgc;
	struct pbr_nexthop_group_cache *pnhgc;
	struct pbr_nexthop_group_cache find;
	struct nexthop *nhop;

	nhgc = nhgc_find(name);
	if (!nhgc)
		return;

	memset(&find, 0, sizeof(find));
	snprintf(find.name, sizeof(find.name), "%s", name);
	pnhgc = hash_lookup(pbr_nhg_hash, &find);

	if (!pnhgc) {
		DEBUGD(&pbr_dbg_nht,
		       "%s: Could not find nexthop-group cache w/ name '%s'",
		       __func__, name);
		return;
	}

	for (ALL_NEXTHOPS(nhgc->nhg, nhop)) {
		struct pbr_nexthop_cache lookup;
		struct pbr_nexthop_cache *pnhc;

		lookup.nexthop = *nhop;
		pnhc = hash_lookup(pnhgc->nhh, &lookup);
		if (!pnhc) {
			pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc);
			pnhc->parent = pnhgc;
		}
	}
	pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg);
}

char *pbr_nht_nexthop_make_name(char *name, size_t l,
				uint32_t seqno, char *buffer)
{
	snprintf(buffer, l, "%s%u", name, seqno);
	return buffer;
}

/* Set data derived from nhg in pbrms */
void pbr_nht_set_seq_nhg_data(struct pbr_map_sequence *pbrms,
			      const struct nexthop_group_cmd *nhgc)
{
	const struct nexthop_group *nhg;

	if (!nhgc)
		return;

	nhg = &nhgc->nhg;
	if (!nhg->nexthop)
		return;

	switch (nhg->nexthop->type) {
	case NEXTHOP_TYPE_IPV6:
	case NEXTHOP_TYPE_IPV6_IFINDEX:
		pbrms->family = AF_INET6;
		break;
	case NEXTHOP_TYPE_IPV4:
	case NEXTHOP_TYPE_IPV4_IFINDEX:
		pbrms->family = AF_INET;
	case NEXTHOP_TYPE_IFINDEX:
	case NEXTHOP_TYPE_BLACKHOLE:
		break;
	}
}

/* Configure a routemap sequence to use a given nexthop group */
void pbr_nht_set_seq_nhg(struct pbr_map_sequence *pbrms, const char *name)
{
	struct nexthop_group_cmd *nhgc;

	if (!name)
		return;

	pbrms->nhgrp_name = XSTRDUP(MTYPE_TMP, name);
	pbrms->forwarding_type = PBR_FT_NEXTHOP_GROUP;

	nhgc = nhgc_find(name);
	if (!nhgc)
		return;

	pbr_nht_set_seq_nhg_data(pbrms, nhgc);
}

void pbr_nht_add_individual_nexthop(struct pbr_map_sequence *pbrms,
				    const struct nexthop *nhop)
{
	struct pbr_nexthop_group_cache *pnhgc;
	struct pbr_nexthop_group_cache find;
	struct pbr_nexthop_cache *pnhc;
	struct pbr_nexthop_cache lookup;
	struct nexthop *nh;
	char buf[PBR_NHC_NAMELEN];

	pbrms->nhg = nexthop_group_new();
	pbrms->internal_nhg_name = XSTRDUP(
		MTYPE_TMP,
		pbr_nht_nexthop_make_name(pbrms->parent->name, PBR_NHC_NAMELEN,
					  pbrms->seqno, buf));
	pbrms->forwarding_type = PBR_FT_NEXTHOP_SINGLE;

	nh = nexthop_new();
	memcpy(nh, nhop, sizeof(*nh));

	nexthop_group_add_sorted(pbrms->nhg, nh);

	memset(&find, 0, sizeof(find));
	pbr_nht_nexthop_make_name(pbrms->parent->name, PBR_NHC_NAMELEN,
				  pbrms->seqno, find.name);

	if (!pbr_nht_has_unallocated_table()) {
		zlog_warn(
			"%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
			__func__, find.name);
		return;
	}

	if (!pbrms->internal_nhg_name)
		pbrms->internal_nhg_name = XSTRDUP(MTYPE_TMP, find.name);

	pnhgc = hash_get(pbr_nhg_hash, &find, pbr_nhgc_alloc);

	lookup.nexthop = *pbrms->nhg->nexthop;
	pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc);
	pnhc->parent = pnhgc;
	if (nhop->vrf_id != VRF_DEFAULT) {
		struct vrf *vrf = vrf_lookup_by_id(nhop->vrf_id);

		if (vrf)
			strlcpy(pnhc->vrf_name, vrf->name,
				sizeof(pnhc->vrf_name));
	}

	if (nhop->ifindex != 0) {
		struct interface *ifp =
			if_lookup_by_index(nhop->ifindex, nhop->vrf_id);

		if (ifp)
			strlcpy(pnhc->intf_name, ifp->name,
				sizeof(pnhc->intf_name));
	}
	pbr_nht_install_nexthop_group(pnhgc, *pbrms->nhg);
}

static void pbr_nht_release_individual_nexthop(struct pbr_map_sequence *pbrms)
{
	struct pbr_nexthop_group_cache *pnhgc;
	struct pbr_nexthop_group_cache find;
	struct pbr_nexthop_cache *pnhc;
	struct pbr_nexthop_cache lup;
	struct nexthop *nh;
	enum nexthop_types_t nh_type = 0;

	memset(&find, 0, sizeof(find));
	snprintf(find.name, sizeof(find.name), "%s", pbrms->internal_nhg_name);
	pnhgc = hash_lookup(pbr_nhg_hash, &find);

	nh = pbrms->nhg->nexthop;
	nh_type = nh->type;
	lup.nexthop = *nh;
	pnhc = hash_lookup(pnhgc->nhh, &lup);
	pnhc->parent = NULL;
	hash_release(pnhgc->nhh, pnhc);
	pbr_nh_delete(&pnhc);
	pbr_nht_uninstall_nexthop_group(pnhgc, *pbrms->nhg, nh_type);

	hash_release(pbr_nhg_hash, pnhgc);
	pbr_nhgc_delete(pnhgc);

	nexthop_group_delete(&pbrms->nhg);
	XFREE(MTYPE_TMP, pbrms->internal_nhg_name);
}

void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence *pbrms)
{
	pbr_map_delete_nexthops(pbrms);

	pbr_nht_release_individual_nexthop(pbrms);
}

struct pbr_nexthop_group_cache *pbr_nht_add_group(const char *name)
{
	struct nexthop *nhop;
	struct nexthop_group_cmd *nhgc;
	struct pbr_nexthop_group_cache *pnhgc;
	struct pbr_nexthop_group_cache lookup;

	if (!pbr_nht_has_unallocated_table()) {
		zlog_warn(
			"%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
			__func__, name);
		return NULL;
	}

	nhgc = nhgc_find(name);

	if (!nhgc) {
		DEBUGD(&pbr_dbg_nht, "%s: Could not find nhgc with name: %s",
		       __func__, name);
		return NULL;
	}

	snprintf(lookup.name, sizeof(lookup.name), "%s", name);
	pnhgc = hash_get(pbr_nhg_hash, &lookup, pbr_nhgc_alloc);
	DEBUGD(&pbr_dbg_nht, "%s: Retrieved NHGC @ %p", __func__, pnhgc);

	for (ALL_NEXTHOPS(nhgc->nhg, nhop)) {
		struct pbr_nexthop_cache lookupc;
		struct pbr_nexthop_cache *pnhc;

		lookupc.nexthop = *nhop;
		pnhc = hash_lookup(pnhgc->nhh, &lookupc);
		if (!pnhc) {
			pnhc = hash_get(pnhgc->nhh, &lookupc, pbr_nh_alloc);
			pnhc->parent = pnhgc;
		}
	}

	return pnhgc;
}

void pbr_nht_delete_group(const char *name)
{
	struct pbr_map_sequence *pbrms;
	struct listnode *snode;
	struct pbr_map *pbrm;
	struct pbr_nexthop_group_cache pnhgc_find;
	struct pbr_nexthop_group_cache *pnhgc;

	RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
		for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, snode, pbrms)) {
			if (pbrms->nhgrp_name
			    && strmatch(pbrms->nhgrp_name, name)) {
				pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS;
				pbrms->nhg = NULL;
				pbrms->internal_nhg_name = NULL;
				pbrm->valid = false;
			}
		}
	}

	strlcpy(pnhgc_find.name, name, sizeof(pnhgc_find.name));
	pnhgc = hash_release(pbr_nhg_hash, &pnhgc_find);

	/*
	 * Ignore deletions of nh we did not / could not allocate nhgc for
	 * Occurs when PBR table range is full but new nhg keep coming in
	 */
	if (!pnhgc)
		return;

	/* Remove and recalculate the next table id */
	hash_release(pbr_nhg_allocated_id_hash, pnhgc);
	pbr_nht_update_next_unallocated_table_id();

	pbr_nhgc_delete(pnhgc);
}

bool pbr_nht_nexthop_valid(struct nexthop_group *nhg)
{
	DEBUGD(&pbr_dbg_nht, "%s: %p", __func__, nhg);
	return true;
}

bool pbr_nht_nexthop_group_valid(const char *name)
{
	struct pbr_nexthop_group_cache *pnhgc;
	struct pbr_nexthop_group_cache lookup;

	DEBUGD(&pbr_dbg_nht, "%s: %s", __func__, name);

	snprintf(lookup.name, sizeof(lookup.name), "%s", name);
	pnhgc = hash_get(pbr_nhg_hash, &lookup, NULL);
	if (!pnhgc)
		return false;
	DEBUGD(&pbr_dbg_nht, "%s:    %d %d", __func__, pnhgc->valid,
	       pnhgc->installed);
	if (pnhgc->valid && pnhgc->installed)
		return true;

	return false;
}

struct pbr_nht_individual {
	struct zapi_route *nhr;
	struct interface *ifp;
	struct pbr_vrf *pbr_vrf;
	struct pbr_nexthop_cache *pnhc;
	vrf_id_t old_vrf_id;

	bool valid;

	bool nhr_matched;
};

static bool
pbr_nht_individual_nexthop_gw_update(struct pbr_nexthop_cache *pnhc,
				     struct pbr_nht_individual *pnhi)
{
	bool is_valid = pnhc->valid;

	/*
	 * If we have an interface down event, let's note that
	 * it is happening and find all the nexthops that depend
	 * on that interface.  As that if we have an interface
	 * flapping fast enough it means that zebra might turn
	 * those nexthop tracking events into a no-update
	 * So let's search and do the right thing on the
	 * interface event.
	 */
	if (!pnhi->nhr) {
		switch (pnhc->nexthop.type) {
		case NEXTHOP_TYPE_BLACKHOLE:
		case NEXTHOP_TYPE_IPV4:
		case NEXTHOP_TYPE_IPV6:
			goto done;
		case NEXTHOP_TYPE_IFINDEX:
		case NEXTHOP_TYPE_IPV4_IFINDEX:
		case NEXTHOP_TYPE_IPV6_IFINDEX:
			if (pnhc->nexthop.ifindex == pnhi->ifp->ifindex)
				is_valid = if_is_up(pnhi->ifp);
			goto done;
		}

		goto done;
	}

	switch (pnhi->nhr->prefix.family) {
	case AF_INET:
		if (pnhc->nexthop.gate.ipv4.s_addr
		    != pnhi->nhr->prefix.u.prefix4.s_addr)
			goto done; /* Unrelated change */
		break;
	case AF_INET6:
		if (memcmp(&pnhc->nexthop.gate.ipv6,
			   &pnhi->nhr->prefix.u.prefix6, 16)
		    != 0)
			goto done; /* Unrelated change */
		break;
	}

	pnhi->nhr_matched = true;
	if (!pnhi->nhr->nexthop_num) {
		is_valid = false;
		goto done;
	}

	if (pnhc->nexthop.type == NEXTHOP_TYPE_IPV4_IFINDEX
	    || pnhc->nexthop.type == NEXTHOP_TYPE_IPV6_IFINDEX) {

		/* GATEWAY_IFINDEX type shouldn't resolve to group */
		if (pnhi->nhr->nexthop_num > 1) {
			is_valid = false;
			goto done;
		}

		/* If whatever we resolved to wasn't on the interface we
		 * specified. (i.e. not a connected route), its invalid.
		 */
		if (pnhi->nhr->nexthops[0].ifindex != pnhc->nexthop.ifindex) {
			is_valid = false;
			goto done;
		}
	}

	is_valid = true;

done:
	pnhc->valid = is_valid;

	return pnhc->valid;
}

static bool
pbr_nht_individual_nexthop_interface_update(struct pbr_nexthop_cache *pnhc,
					    struct pbr_nht_individual *pnhi)
{
	bool is_valid = pnhc->valid;

	if (!pnhi->ifp) /* It doesn't care about non-interface updates */
		goto done;

	if (pnhc->nexthop.ifindex
	    != pnhi->ifp->ifindex) /* Un-related interface */
		goto done;

	pnhi->nhr_matched = true;
	is_valid = !!if_is_up(pnhi->ifp);

done:
	pnhc->valid = is_valid;

	return pnhc->valid;
}

/* Given this update either from interface or nexthop tracking, re-validate this
 * nexthop.
 *
 * If the update is un-related, the subroutines shoud just return their cached
 * valid state.
 */
static void pbr_nht_individual_nexthop_update(struct pbr_nexthop_cache *pnhc,
					      struct pbr_nht_individual *pnhi)
{
	assert(pnhi->nhr || pnhi->ifp); /* Either nexthop or interface update */

	switch (pnhc->nexthop.type) {
	case NEXTHOP_TYPE_IFINDEX:
		pbr_nht_individual_nexthop_interface_update(pnhc, pnhi);
		break;
	case NEXTHOP_TYPE_IPV6_IFINDEX:
		if (IN6_IS_ADDR_LINKLOCAL(&pnhc->nexthop.gate.ipv6)) {
			pbr_nht_individual_nexthop_interface_update(pnhc, pnhi);
			break;
		}
		/* Intentional fall thru */
	case NEXTHOP_TYPE_IPV4_IFINDEX:
	case NEXTHOP_TYPE_IPV4:
	case NEXTHOP_TYPE_IPV6:
		pbr_nht_individual_nexthop_gw_update(pnhc, pnhi);
		break;
	case NEXTHOP_TYPE_BLACKHOLE:
		pnhc->valid = true;
		break;
	}
}

static void pbr_nht_individual_nexthop_update_lookup(struct hash_bucket *b,
						     void *data)
{
	struct pbr_nexthop_cache *pnhc = b->data;
	struct pbr_nht_individual *pnhi = data;
	bool old_valid;

	old_valid = pnhc->valid;

	pbr_nht_individual_nexthop_update(pnhc, pnhi);

	DEBUGD(&pbr_dbg_nht, "    Found %pFX: old: %d new: %d",
	       &pnhi->nhr->prefix, old_valid, pnhc->valid);

	if (pnhc->valid)
		pnhi->valid = true;
}

static void pbr_nexthop_group_cache_iterate_to_group(struct hash_bucket *b,
						     void *data)
{
	struct pbr_nexthop_cache *pnhc = b->data;
	struct nexthop_group *nhg = data;
	struct nexthop *nh = NULL;

	copy_nexthops(&nh, &pnhc->nexthop, NULL);

	_nexthop_add(&nhg->nexthop, nh);
}

static void
pbr_nexthop_group_cache_to_nexthop_group(struct nexthop_group *nhg,
					 struct pbr_nexthop_group_cache *pnhgc)
{
	hash_iterate(pnhgc->nhh, pbr_nexthop_group_cache_iterate_to_group, nhg);
}

static void pbr_nht_nexthop_update_lookup(struct hash_bucket *b, void *data)
{
	struct pbr_nexthop_group_cache *pnhgc = b->data;
	struct pbr_nht_individual pnhi = {};
	struct nexthop_group nhg = {};
	bool old_valid;

	old_valid = pnhgc->valid;

	pnhi.nhr = (struct zapi_route *)data;
	pnhi.valid = false;
	pnhi.nhr_matched = false;
	hash_iterate(pnhgc->nhh, pbr_nht_individual_nexthop_update_lookup,
		     &pnhi);

	if (!pnhi.nhr_matched)
		return;

	/*
	 * If any of the specified nexthops are valid we are valid
	 */
	pnhgc->valid = !!pnhi.valid;

	pbr_nexthop_group_cache_to_nexthop_group(&nhg, pnhgc);

	if (pnhgc->valid)
		pbr_nht_install_nexthop_group(pnhgc, nhg);
	else
		pbr_nht_uninstall_nexthop_group(pnhgc, nhg, 0);

	/* Don't need copied nexthops anymore */
	nexthops_free(nhg.nexthop);

	if (old_valid != pnhgc->valid)
		pbr_map_check_nh_group_change(pnhgc->name);
}

void pbr_nht_nexthop_update(struct zapi_route *nhr)
{
	hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_update_lookup, nhr);
}

struct nhrc_vrf_info {
	struct pbr_vrf *pbr_vrf;
	uint32_t old_vrf_id;
	struct nhrc *nhrc;
};

static int pbr_nht_nhrc_vrf_change(struct hash_bucket *b, void *data)
{
	struct nhrc *nhrc = b->data;
	struct nhrc_vrf_info *nhrcvi = data;

	if (nhrc->nexthop.vrf_id == nhrcvi->old_vrf_id) {
		nhrcvi->nhrc = nhrc;
		return HASHWALK_ABORT;
	}

	return HASHWALK_CONTINUE;
}

static int pbr_nht_individual_nexthop_vrf_handle(struct hash_bucket *b,
						 void *data)
{
	struct pbr_nexthop_cache *pnhc = b->data;
	struct pbr_nht_individual *pnhi = data;

	if (pnhc->looked_at == true)
		return HASHWALK_CONTINUE;

	if (pnhc->nexthop.vrf_id == VRF_DEFAULT)
		return HASHWALK_CONTINUE;

	if (strncmp(pnhc->vrf_name, pbr_vrf_name(pnhi->pbr_vrf),
		    sizeof(pnhc->vrf_name))
	    == 0) {
		pnhi->pnhc = pnhc;

		if (pnhc->nexthop.vrf_id != pbr_vrf_id(pnhi->pbr_vrf)) {
			struct nhrc_vrf_info nhrcvi;

			memset(&nhrcvi, 0, sizeof(nhrcvi));
			nhrcvi.pbr_vrf = pnhi->pbr_vrf;
			nhrcvi.old_vrf_id = pnhc->nexthop.vrf_id;

			pnhi->nhr_matched = true;
			pnhi->old_vrf_id = pnhc->nexthop.vrf_id;

			do {
				nhrcvi.nhrc = NULL;
				hash_walk(pbr_nhrc_hash,
					  pbr_nht_nhrc_vrf_change, &nhrcvi);
				if (nhrcvi.nhrc) {
					hash_release(pbr_nhrc_hash,
						     nhrcvi.nhrc);
					nhrcvi.nhrc->nexthop.vrf_id =
						pbr_vrf_id(pnhi->pbr_vrf);
					(void)hash_get(pbr_nhrc_hash,
						       nhrcvi.nhrc,
						       hash_alloc_intern);
					pbr_send_rnh(&nhrcvi.nhrc->nexthop, true);
				}
			} while (nhrcvi.nhrc);
		}

		pnhc->looked_at = true;
		return HASHWALK_ABORT;
	}

	return HASHWALK_CONTINUE;
}

static void pbr_nht_clear_looked_at(struct hash_bucket *b, void *data)
{
	struct pbr_nexthop_cache *pnhc = b->data;

	pnhc->looked_at = false;
}

static void pbr_nht_nexthop_vrf_handle(struct hash_bucket *b, void *data)
{
	struct pbr_nexthop_group_cache *pnhgc = b->data;
	struct pbr_vrf *pbr_vrf = data;
	struct pbr_nht_individual pnhi = {};

	hash_iterate(pnhgc->nhh, pbr_nht_clear_looked_at, NULL);
	memset(&pnhi, 0, sizeof(pnhi));
	pnhi.pbr_vrf = pbr_vrf;
	do {
		struct pbr_nexthop_cache *pnhc;

		pnhi.pnhc = NULL;
		hash_walk(pnhgc->nhh, pbr_nht_individual_nexthop_vrf_handle,
			  &pnhi);

		if (!pnhi.pnhc)
			continue;

		pnhc = pnhi.pnhc;
		pnhc->nexthop.vrf_id = pnhi.old_vrf_id;
		pnhi.pnhc = hash_release(pnhgc->nhh, pnhi.pnhc);
		if (pnhi.pnhc) {
			pnhi.pnhc->nexthop.vrf_id = pbr_vrf_id(pbr_vrf);

			(void)hash_get(pnhgc->nhh, pnhi.pnhc,
				       hash_alloc_intern);
		} else
			pnhc->nexthop.vrf_id = pbr_vrf_id(pbr_vrf);

		pbr_map_check_vrf_nh_group_change(pnhgc->name, pbr_vrf,
						  pnhi.old_vrf_id);
	} while (pnhi.pnhc);
}

void pbr_nht_vrf_update(struct pbr_vrf *pbr_vrf)
{
	hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_vrf_handle, pbr_vrf);
}

static void pbr_nht_individual_nexthop_interface_handle(struct hash_bucket *b,
							void *data)
{
	struct pbr_nexthop_cache *pnhc = b->data;
	struct pbr_nht_individual *pnhi = data;

	if (pnhc->nexthop.ifindex == 0)
		return;

	if ((strncmp(pnhc->intf_name, pnhi->ifp->name, sizeof(pnhc->intf_name))
	     == 0)
	    && pnhc->nexthop.ifindex != pnhi->ifp->ifindex)
		pnhi->pnhc = pnhc;
}

static void pbr_nht_nexthop_interface_handle(struct hash_bucket *b, void *data)
{
	struct pbr_nexthop_group_cache *pnhgc = b->data;
	struct interface *ifp = data;
	struct pbr_nht_individual pnhi = {};
	struct nhrc *nhrc;
	uint32_t old_ifindex;

	do {
		memset(&pnhi, 0, sizeof(pnhi));
		pnhi.ifp = ifp;
		hash_iterate(pnhgc->nhh,
			     pbr_nht_individual_nexthop_interface_handle,
			     &pnhi);

		if (!pnhi.pnhc)
			continue;

		pnhi.pnhc = hash_release(pnhgc->nhh, pnhi.pnhc);
		old_ifindex = pnhi.pnhc->nexthop.ifindex;

		nhrc = hash_lookup(pbr_nhrc_hash, &pnhi.pnhc->nexthop);
		if (nhrc) {
			hash_release(pbr_nhrc_hash, nhrc);
			nhrc->nexthop.ifindex = ifp->ifindex;
			(void)hash_get(pbr_nhrc_hash, nhrc, hash_alloc_intern);
		}
		pnhi.pnhc->nexthop.ifindex = ifp->ifindex;

		(void)hash_get(pnhgc->nhh, pnhi.pnhc, hash_alloc_intern);

		pbr_map_check_interface_nh_group_change(pnhgc->name, ifp,
							old_ifindex);
	} while (pnhi.pnhc);
}

void pbr_nht_interface_update(struct interface *ifp)
{
	hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_interface_handle, ifp);
}

static void
pbr_nht_individual_nexthop_interface_update_lookup(struct hash_bucket *b,
						   void *data)
{
	struct pbr_nexthop_cache *pnhc = b->data;
	struct pbr_nht_individual *pnhi = data;
	bool old_valid;

	old_valid = pnhc->valid;

	pbr_nht_individual_nexthop_update(pnhc, pnhi);

	DEBUGD(&pbr_dbg_nht, "    Found %s: old: %d new: %d", pnhi->ifp->name,
	       old_valid, pnhc->valid);

	if (pnhc->valid)
		pnhi->valid = true;
}

static void pbr_nht_nexthop_interface_update_lookup(struct hash_bucket *b,
						    void *data)
{
	struct pbr_nexthop_group_cache *pnhgc = b->data;
	struct pbr_nht_individual pnhi = {};
	struct nexthop_group nhg = {};
	bool old_valid;

	old_valid = pnhgc->valid;

	pnhi.ifp = data;
	pnhi.valid = false;
	hash_iterate(pnhgc->nhh,
		     pbr_nht_individual_nexthop_interface_update_lookup, &pnhi);

	/*
	 * If any of the specified nexthops are valid we are valid
	 */
	pnhgc->valid = pnhi.valid;

	pbr_nexthop_group_cache_to_nexthop_group(&nhg, pnhgc);

	if (pnhgc->valid)
		pbr_nht_install_nexthop_group(pnhgc, nhg);
	else
		pbr_nht_uninstall_nexthop_group(pnhgc, nhg, 0);

	nexthops_free(nhg.nexthop);

	if (old_valid != pnhgc->valid)
		pbr_map_check_nh_group_change(pnhgc->name);
}

void pbr_nht_nexthop_interface_update(struct interface *ifp)
{
	hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_interface_update_lookup,
		     ifp);
}

static bool pbr_nhg_allocated_id_hash_equal(const void *arg1, const void *arg2)
{
	const struct pbr_nexthop_group_cache *left, *right;

	left = (const struct pbr_nexthop_group_cache *)arg1;
	right = (const struct pbr_nexthop_group_cache *)arg2;

	return left->table_id == right->table_id;
}

static uint32_t pbr_nhg_allocated_id_hash_key(const void *arg)
{
	const struct pbr_nexthop_group_cache *nhgc = arg;

	/* table_id makes elements in this hash unique */
	return nhgc->table_id;
}

static uint32_t pbr_nhg_hash_key(const void *arg)
{
	const struct pbr_nexthop_group_cache *nhgc = arg;

	return jhash(&nhgc->name, strlen(nhgc->name), 0x52c34a96);
}

static bool pbr_nhg_hash_equal(const void *arg1, const void *arg2)
{
	const struct pbr_nexthop_group_cache *nhgc1 =
		(const struct pbr_nexthop_group_cache *)arg1;
	const struct pbr_nexthop_group_cache *nhgc2 =
		(const struct pbr_nexthop_group_cache *)arg2;

	return !strcmp(nhgc1->name, nhgc2->name);
}

uint32_t pbr_nht_find_next_unallocated_table_id(void)
{
	struct pbr_nexthop_group_cache iter;

	/*
	 * Find the smallest unallocated table id
	 * This can be non-trivial considering nhg removals / shifting upper &
	 * lower bounds, so start at the lowest in the range and continue until
	 * an unallocated space is found
	 */
	for (iter.table_id = pbr_nhg_low_table;
	     iter.table_id < pbr_nhg_high_table; ++iter.table_id)
		if (!hash_lookup(pbr_nhg_allocated_id_hash, &iter))
			return iter.table_id;

	/* Configured range is full, cannot install anywhere */
	return 0;
}

bool pbr_nht_has_unallocated_table(void)
{
	return !!pbr_next_unallocated_table_id;
}

void pbr_nht_update_next_unallocated_table_id(void)
{
	pbr_next_unallocated_table_id =
		pbr_nht_find_next_unallocated_table_id();
}

uint32_t pbr_nht_reserve_next_table_id(struct pbr_nexthop_group_cache *nhgc)
{
	/* Nothing to reserve if all tables in range already used */
	if (!pbr_next_unallocated_table_id)
		return 0;

	/* Reserve this table id */
	nhgc->table_id = pbr_next_unallocated_table_id;

	/* Mark table id as allocated in id-indexed hash */
	(void)hash_get(pbr_nhg_allocated_id_hash, nhgc, hash_alloc_intern);

	/* Pre-compute the next unallocated table id */
	pbr_nht_update_next_unallocated_table_id();

	/* Present caller with reserved table id */
	return nhgc->table_id;
}

void pbr_nht_set_tableid_range(uint32_t low, uint32_t high)
{
	pbr_nhg_low_table = low;
	pbr_nhg_high_table = high;

	/* Re-compute next unallocated id within new range */
	pbr_nht_update_next_unallocated_table_id();
}

void pbr_nht_write_table_range(struct vty *vty)
{
	if (pbr_nhg_low_table != PBR_NHT_DEFAULT_LOW_TABLEID
	    || pbr_nhg_high_table != PBR_NHT_DEFAULT_HIGH_TABLEID) {
		vty_out(vty, "pbr table range %u %u\n", pbr_nhg_low_table,
			pbr_nhg_high_table);
	}
}

uint32_t pbr_nht_get_next_rule(uint32_t seqno)
{
	return seqno + pbr_nhg_low_rule - 1;
}
void pbr_nht_set_rule_range(uint32_t low, uint32_t high)
{
	pbr_nhg_low_rule = low;
	pbr_nhg_high_rule = high;
}

void pbr_nht_write_rule_range(struct vty *vty)
{
	if (pbr_nhg_low_rule != PBR_NHT_DEFAULT_LOW_RULE
	    || pbr_nhg_high_rule != PBR_NHT_DEFAULT_HIGH_RULE) {
		vty_out(vty, "pbr rule range %u %u\n", pbr_nhg_low_rule,
			pbr_nhg_high_rule);
	}
}

uint32_t pbr_nht_get_table(const char *name)
{
	struct pbr_nexthop_group_cache find;
	struct pbr_nexthop_group_cache *pnhgc;

	memset(&find, 0, sizeof(find));
	snprintf(find.name, sizeof(find.name), "%s", name);
	pnhgc = hash_lookup(pbr_nhg_hash, &find);

	if (!pnhgc) {
		DEBUGD(&pbr_dbg_nht,
		       "%s: Could not find nexthop-group cache w/ name '%s'",
		       __func__, name);
		return 5000;
	}

	return pnhgc->table_id;
}

bool pbr_nht_get_installed(const char *name)
{
	struct pbr_nexthop_group_cache find;
	struct pbr_nexthop_group_cache *pnhgc;

	memset(&find, 0, sizeof(find));
	snprintf(find.name, sizeof(find.name), "%s", name);

	pnhgc = hash_lookup(pbr_nhg_hash, &find);

	if (!pnhgc)
		return false;

	return pnhgc->installed;
}

static void pbr_nht_show_nhg_nexthops(struct hash_bucket *b, void *data)
{
	struct pbr_nexthop_cache *pnhc = b->data;
	struct vty *vty = data;

	vty_out(vty, "\tValid: %d ", pnhc->valid);
	nexthop_group_write_nexthop(vty, &pnhc->nexthop);
}

static void pbr_nht_json_nhg_nexthops(struct hash_bucket *b, void *data)
{
	struct pbr_nexthop_cache *pnhc = b->data;
	json_object *all_hops = data;
	json_object *this_hop;

	this_hop = json_object_new_object();
	nexthop_group_json_nexthop(this_hop, &pnhc->nexthop);
	json_object_boolean_add(this_hop, "valid", pnhc->valid);

	json_object_array_add(all_hops, this_hop);
}

struct pbr_nht_show {
	struct vty *vty;
	json_object *json;
	const char *name;
};

static void pbr_nht_show_nhg(struct hash_bucket *b, void *data)
{
	struct pbr_nexthop_group_cache *pnhgc = b->data;
	struct pbr_nht_show *pns = data;
	struct vty *vty;

	if (pns->name && strcmp(pns->name, pnhgc->name) != 0)
		return;

	vty = pns->vty;
	vty_out(vty, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n",
		pnhgc->name, pnhgc->table_id, pnhgc->valid, pnhgc->installed);

	hash_iterate(pnhgc->nhh, pbr_nht_show_nhg_nexthops, vty);
}

static void pbr_nht_json_nhg(struct hash_bucket *b, void *data)
{
	struct pbr_nexthop_group_cache *pnhgc = b->data;
	struct pbr_nht_show *pns = data;
	json_object *j, *this_group, *group_hops;

	if (pns->name && strcmp(pns->name, pnhgc->name) != 0)
		return;

	j = pns->json;
	this_group = json_object_new_object();

	if (!j || !this_group)
		return;

	json_object_int_add(this_group, "id", pnhgc->table_id);
	json_object_string_add(this_group, "name", pnhgc->name);
	json_object_boolean_add(this_group, "valid", pnhgc->valid);
	json_object_boolean_add(this_group, "installed", pnhgc->installed);

	group_hops = json_object_new_array();

	if (group_hops) {
		hash_iterate(pnhgc->nhh, pbr_nht_json_nhg_nexthops, group_hops);
		json_object_object_add(this_group, "nexthops", group_hops);
	}

	json_object_array_add(j, this_group);
}

void pbr_nht_show_nexthop_group(struct vty *vty, const char *name)
{
	struct pbr_nht_show pns;

	pns.vty = vty;
	pns.name = name;

	hash_iterate(pbr_nhg_hash, pbr_nht_show_nhg, &pns);
}

void pbr_nht_json_nexthop_group(json_object *j, const char *name)
{
	struct pbr_nht_show pns;

	pns.name = name;
	pns.json = j;

	hash_iterate(pbr_nhg_hash, pbr_nht_json_nhg, &pns);
}

void pbr_nht_init(void)
{
	pbr_nhg_hash = hash_create_size(
		16, pbr_nhg_hash_key, pbr_nhg_hash_equal, "PBR NHG Cache Hash");
	pbr_nhrc_hash =
		hash_create_size(16, (unsigned int (*)(const void *))nexthop_hash,
				 pbr_nhrc_hash_equal, "PBR NH Hash");
	pbr_nhg_allocated_id_hash = hash_create_size(
		16, pbr_nhg_allocated_id_hash_key,
		pbr_nhg_allocated_id_hash_equal, "PBR Allocated Table Hash");

	pbr_nhg_low_table = PBR_NHT_DEFAULT_LOW_TABLEID;
	pbr_nhg_high_table = PBR_NHT_DEFAULT_HIGH_TABLEID;
	pbr_nhg_low_rule = PBR_NHT_DEFAULT_LOW_RULE;
	pbr_nhg_high_rule = PBR_NHT_DEFAULT_HIGH_RULE;

	/* First unallocated table is lowest in range on init */
	pbr_next_unallocated_table_id = PBR_NHT_DEFAULT_LOW_TABLEID;
}