]> git.proxmox.com Git - mirror_frr.git/blame - pbrd/pbr_nht.c
*: require ISO C11 (or C++11)
[mirror_frr.git] / pbrd / pbr_nht.c
CommitLineData
e5c83d9b
DS
1/*
2 * PBR-nht Code
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * FRR is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * FRR is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#include <zebra.h>
21
22#include <log.h>
23#include <nexthop.h>
50d89650
SW
24#include "nexthop_group.h"
25#include "nexthop_group_private.h"
e5c83d9b
DS
26#include <hash.h>
27#include <jhash.h>
28#include <vty.h>
29#include <zclient.h>
b13e5ad6 30#include <debug.h>
e5c83d9b
DS
31
32#include "pbrd/pbr_nht.h"
33#include "pbrd/pbr_map.h"
e5c83d9b
DS
34#include "pbrd/pbr_zebra.h"
35#include "pbrd/pbr_memory.h"
36#include "pbrd/pbr_debug.h"
37
38DEFINE_MTYPE_STATIC(PBRD, PBR_NHG, "PBR Nexthop Groups")
39
fcf29c69 40struct hash *pbr_nhg_hash;
b13e5ad6 41static struct hash *pbr_nhrc_hash;
e5c83d9b
DS
42
43static uint32_t pbr_nhg_low_table;
44static uint32_t pbr_nhg_high_table;
45static uint32_t pbr_nhg_low_rule;
46static uint32_t pbr_nhg_high_rule;
47static bool nhg_tableid[65535];
48
b13e5ad6
DS
49static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
50 struct nexthop_group nhg);
ff9799c3
DS
51static void
52pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
53 struct nexthop_group nhg,
aafac994 54 enum nexthop_types_t nh_type);
b13e5ad6
DS
55
56/*
57 * Nexthop refcount.
58 */
59struct nhrc {
60 struct nexthop nexthop;
61 unsigned int refcount;
62};
63
64/* Hash functions for pbr_nhrc_hash ---------------------------------------- */
65
66static void *pbr_nhrc_hash_alloc(void *p)
67{
68 struct nhrc *nhrc = XCALLOC(MTYPE_PBR_NHG, sizeof(struct nhrc));
69 nhrc->nexthop = *(struct nexthop *)p;
ad9255f8
SW
70 nhrc->nexthop.next = NULL;
71 nhrc->nexthop.prev = NULL;
b13e5ad6
DS
72 return nhrc;
73}
74
74df8d6d 75static bool pbr_nhrc_hash_equal(const void *arg1, const void *arg2)
b13e5ad6
DS
76{
77 const struct nexthop *nh1, *nh2;
78
79 nh1 = arg1;
80 nh2 = arg2;
81
82 return nexthop_same(nh1, nh2);
83}
84
85/* ------------------------------------------------------------------------- */
86
e5c83d9b
DS
87static void *pbr_nh_alloc(void *p)
88{
89 struct pbr_nexthop_cache *new;
90 struct pbr_nexthop_cache *pnhc = (struct pbr_nexthop_cache *)p;
b13e5ad6 91 struct nhrc *nhrc;
e5c83d9b
DS
92
93 new = XCALLOC(MTYPE_PBR_NHG, sizeof(*new));
734bf907
DS
94 nhrc = hash_get(pbr_nhrc_hash, &pnhc->nexthop, pbr_nhrc_hash_alloc);
95 new->nexthop = nhrc->nexthop;
b13e5ad6
DS
96
97 /* Decremented again in pbr_nh_delete */
98 ++nhrc->refcount;
e5c83d9b 99
15569c58 100 DEBUGD(&pbr_dbg_nht, "%s: Sending nexthop to Zebra", __func__);
e5c83d9b 101
734bf907 102 pbr_send_rnh(&new->nexthop, true);
e5c83d9b
DS
103
104 new->valid = false;
105 return new;
106}
107
108static void pbr_nh_delete(struct pbr_nexthop_cache **pnhc)
109{
b13e5ad6
DS
110 struct nhrc *nhrc;
111
734bf907 112 nhrc = hash_lookup(pbr_nhrc_hash, &((*pnhc)->nexthop));
b13e5ad6
DS
113
114 if (nhrc)
115 --nhrc->refcount;
116 if (!nhrc || nhrc->refcount == 0) {
117 DEBUGD(&pbr_dbg_nht, "%s: Removing nexthop from Zebra",
15569c58 118 __func__);
734bf907 119 pbr_send_rnh(&((*pnhc)->nexthop), false);
b13e5ad6
DS
120 }
121 if (nhrc && nhrc->refcount == 0) {
122 hash_release(pbr_nhrc_hash, nhrc);
123 XFREE(MTYPE_PBR_NHG, nhrc);
124 }
e5c83d9b
DS
125
126 XFREE(MTYPE_PBR_NHG, *pnhc);
127}
128
e3b78da8 129static void pbr_nh_delete_iterate(struct hash_bucket *b, void *p)
b13e5ad6
DS
130{
131 pbr_nh_delete((struct pbr_nexthop_cache **)&b->data);
132}
133
d8b87afe 134static uint32_t pbr_nh_hash_key(const void *arg)
e5c83d9b
DS
135{
136 uint32_t key;
d8b87afe 137 const struct pbr_nexthop_cache *pbrnc = arg;
e5c83d9b 138
734bf907 139 key = nexthop_hash(&pbrnc->nexthop);
e5c83d9b
DS
140
141 return key;
142}
143
74df8d6d 144static bool pbr_nh_hash_equal(const void *arg1, const void *arg2)
e5c83d9b
DS
145{
146 const struct pbr_nexthop_cache *pbrnc1 =
147 (const struct pbr_nexthop_cache *)arg1;
148 const struct pbr_nexthop_cache *pbrnc2 =
149 (const struct pbr_nexthop_cache *)arg2;
150
734bf907 151 if (pbrnc1->nexthop.vrf_id != pbrnc2->nexthop.vrf_id)
74df8d6d 152 return false;
e5c83d9b 153
734bf907 154 if (pbrnc1->nexthop.ifindex != pbrnc2->nexthop.ifindex)
74df8d6d 155 return false;
e5c83d9b 156
734bf907 157 if (pbrnc1->nexthop.type != pbrnc2->nexthop.type)
74df8d6d 158 return false;
e5c83d9b 159
734bf907 160 switch (pbrnc1->nexthop.type) {
e5c83d9b 161 case NEXTHOP_TYPE_IFINDEX:
734bf907 162 return pbrnc1->nexthop.ifindex == pbrnc2->nexthop.ifindex;
e5c83d9b
DS
163 case NEXTHOP_TYPE_IPV4_IFINDEX:
164 case NEXTHOP_TYPE_IPV4:
734bf907
DS
165 return pbrnc1->nexthop.gate.ipv4.s_addr
166 == pbrnc2->nexthop.gate.ipv4.s_addr;
e5c83d9b
DS
167 case NEXTHOP_TYPE_IPV6_IFINDEX:
168 case NEXTHOP_TYPE_IPV6:
734bf907
DS
169 return !memcmp(&pbrnc1->nexthop.gate.ipv6,
170 &pbrnc2->nexthop.gate.ipv6, 16);
e5c83d9b 171 case NEXTHOP_TYPE_BLACKHOLE:
734bf907 172 return pbrnc1->nexthop.bh_type == pbrnc2->nexthop.bh_type;
e5c83d9b
DS
173 }
174
175 /*
176 * We should not get here
177 */
74df8d6d 178 return false;
e5c83d9b
DS
179}
180
b13e5ad6
DS
181static void pbr_nhgc_delete(struct pbr_nexthop_group_cache *p)
182{
183 hash_iterate(p->nhh, pbr_nh_delete_iterate, NULL);
184 hash_free(p->nhh);
185 XFREE(MTYPE_PBR_NHG, p);
186}
187
188static void *pbr_nhgc_alloc(void *p)
189{
190 struct pbr_nexthop_group_cache *new;
191 struct pbr_nexthop_group_cache *pnhgc =
192 (struct pbr_nexthop_group_cache *)p;
193
194 new = XCALLOC(MTYPE_PBR_NHG, sizeof(*new));
195
65b88efa 196 strlcpy(new->name, pnhgc->name, sizeof(pnhgc->name));
a4044dc1 197 new->table_id = pbr_nht_get_next_tableid(false);
b13e5ad6 198
15569c58
DA
199 DEBUGD(&pbr_dbg_nht, "%s: NHT: %s assigned Table ID: %u", __func__,
200 new->name, new->table_id);
b13e5ad6
DS
201
202 new->nhh = hash_create_size(8, pbr_nh_hash_key, pbr_nh_hash_equal,
203 "PBR NH Cache Hash");
204 return new;
205}
206
207
e5c83d9b
DS
208void pbr_nhgroup_add_cb(const char *name)
209{
b13e5ad6
DS
210 struct pbr_nexthop_group_cache *pnhgc;
211 struct nexthop_group_cmd *nhgc;
e5c83d9b 212
b13e5ad6 213 nhgc = nhgc_find(name);
68a63f60
QY
214
215 if (!nhgc) {
1d5453d6 216 DEBUGD(&pbr_dbg_nht, "%s: Could not find nhgc with name: %s",
15569c58 217 __func__, name);
68a63f60
QY
218 return;
219 }
220
b13e5ad6 221 pnhgc = pbr_nht_add_group(name);
e5c83d9b 222
a4044dc1
QY
223 if (!pnhgc)
224 return;
225
15569c58 226 DEBUGD(&pbr_dbg_nht, "%s: Added nexthop-group %s", __func__, name);
b13e5ad6 227
b13e5ad6 228 pbr_map_check_nh_group_change(name);
e5c83d9b
DS
229}
230
b13e5ad6 231void pbr_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd *nhgc,
e5c83d9b
DS
232 const struct nexthop *nhop)
233{
b13e5ad6 234 char debugstr[256];
3e300703 235 struct pbr_nexthop_group_cache pnhgc_find = {};
b13e5ad6 236 struct pbr_nexthop_group_cache *pnhgc;
3e300703 237 struct pbr_nexthop_cache pnhc_find = {};
b13e5ad6
DS
238 struct pbr_nexthop_cache *pnhc;
239
a4044dc1
QY
240 if (!pbr_nht_get_next_tableid(true)) {
241 zlog_warn(
242 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
15569c58 243 __func__, nhgc->name);
a4044dc1
QY
244 return;
245 }
246
b13e5ad6
DS
247 /* find pnhgc by name */
248 strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name));
249 pnhgc = hash_get(pbr_nhg_hash, &pnhgc_find, pbr_nhgc_alloc);
e5c83d9b 250
b13e5ad6 251 /* create & insert new pnhc into pnhgc->nhh */
734bf907 252 pnhc_find.nexthop = *nhop;
b13e5ad6 253 pnhc = hash_get(pnhgc->nhh, &pnhc_find, pbr_nh_alloc);
b13e5ad6
DS
254
255 /* set parent pnhgc */
256 pnhc->parent = pnhgc;
e5c83d9b 257
b13e5ad6
DS
258 if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) {
259 nexthop2str(nhop, debugstr, sizeof(debugstr));
260 DEBUGD(&pbr_dbg_nht, "%s: Added %s to nexthop-group %s",
15569c58 261 __func__, debugstr, nhgc->name);
b13e5ad6
DS
262 }
263
264 pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg);
265 pbr_map_check_nh_group_change(nhgc->name);
a106a408 266
cb254f41
SW
267 if (nhop->type == NEXTHOP_TYPE_IFINDEX
268 || (nhop->type == NEXTHOP_TYPE_IPV6_IFINDEX
269 && IN6_IS_ADDR_LINKLOCAL(&nhop->gate.ipv6))) {
a106a408
RW
270 struct interface *ifp;
271
272 ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
273 if (ifp)
274 pbr_nht_nexthop_interface_update(ifp);
275 }
e5c83d9b
DS
276}
277
b13e5ad6 278void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd *nhgc,
e5c83d9b
DS
279 const struct nexthop *nhop)
280{
b13e5ad6 281 char debugstr[256];
3e300703 282 struct pbr_nexthop_group_cache pnhgc_find = {};
b13e5ad6 283 struct pbr_nexthop_group_cache *pnhgc;
3e300703 284 struct pbr_nexthop_cache pnhc_find = {};
b13e5ad6 285 struct pbr_nexthop_cache *pnhc;
aafac994 286 enum nexthop_types_t nh_type = nhop->type;
b13e5ad6
DS
287
288 /* find pnhgc by name */
289 strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name));
a4044dc1 290 pnhgc = hash_lookup(pbr_nhg_hash, &pnhgc_find);
b13e5ad6
DS
291
292 /* delete pnhc from pnhgc->nhh */
734bf907 293 pnhc_find.nexthop = *nhop;
b13e5ad6
DS
294 pnhc = hash_release(pnhgc->nhh, &pnhc_find);
295
296 /* delete pnhc */
297 pbr_nh_delete(&pnhc);
e5c83d9b 298
b13e5ad6
DS
299 if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) {
300 nexthop2str(nhop, debugstr, sizeof(debugstr));
301 DEBUGD(&pbr_dbg_nht, "%s: Removed %s from nexthop-group %s",
15569c58 302 __func__, debugstr, nhgc->name);
b13e5ad6 303 }
e5c83d9b 304
ff9799c3
DS
305 if (pnhgc->nhh->count)
306 pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg);
307 else
aafac994 308 pbr_nht_uninstall_nexthop_group(pnhgc, nhgc->nhg, nh_type);
ff9799c3 309
b13e5ad6 310 pbr_map_check_nh_group_change(nhgc->name);
e5c83d9b
DS
311}
312
313void pbr_nhgroup_delete_cb(const char *name)
314{
15569c58 315 DEBUGD(&pbr_dbg_nht, "%s: Removed nexthop-group %s", __func__, name);
b13e5ad6 316
ff9799c3
DS
317 /* delete group from all pbrms's */
318 pbr_nht_delete_group(name);
319
b13e5ad6 320 pbr_map_check_nh_group_change(name);
e5c83d9b
DS
321}
322
09813729
SW
323static void
324pbr_nht_find_nhg_from_table_update(struct pbr_nexthop_group_cache *pnhgc,
325 uint32_t table_id, bool installed)
326{
327 if (pnhgc->table_id == table_id) {
328 DEBUGD(&pbr_dbg_nht, "%s: %s: Table ID (%u) matches %s",
329 __func__, (installed ? "install" : "remove"), table_id,
330 pnhgc->name);
331
332 pnhgc->installed = installed;
333 pnhgc->valid = installed;
334 pbr_map_schedule_policy_from_nhg(pnhgc->name, pnhgc->installed);
335 }
336}
337
e3b78da8 338static void pbr_nht_find_nhg_from_table_install(struct hash_bucket *b,
e5c83d9b
DS
339 void *data)
340{
341 struct pbr_nexthop_group_cache *pnhgc =
342 (struct pbr_nexthop_group_cache *)b->data;
09813729
SW
343 uint32_t table_id = *(uint32_t *)data;
344
345 pbr_nht_find_nhg_from_table_update(pnhgc, table_id, true);
e5c83d9b
DS
346}
347
348void pbr_nht_route_installed_for_table(uint32_t table_id)
349{
350 hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_install,
351 &table_id);
352}
353
e3b78da8 354static void pbr_nht_find_nhg_from_table_remove(struct hash_bucket *b,
e5c83d9b
DS
355 void *data)
356{
09813729
SW
357 struct pbr_nexthop_group_cache *pnhgc =
358 (struct pbr_nexthop_group_cache *)b->data;
359 uint32_t table_id = *(uint32_t *)data;
360
361 pbr_nht_find_nhg_from_table_update(pnhgc, table_id, false);
e5c83d9b
DS
362}
363
364void pbr_nht_route_removed_for_table(uint32_t table_id)
365{
366 hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_remove,
367 &table_id);
368}
369
370/*
371 * Loop through all nexthops in a nexthop group to check that they are all the
372 * same. If they are not all the same, log this peculiarity.
373 *
374 * nhg
375 * The nexthop group to check
376 *
377 * Returns:
378 * - AFI of last nexthop in the group
379 * - AFI_MAX on error
380 */
ff9799c3 381static afi_t pbr_nht_which_afi(struct nexthop_group nhg,
aafac994 382 enum nexthop_types_t nh_type)
e5c83d9b
DS
383{
384 struct nexthop *nexthop;
385 afi_t install_afi = AFI_MAX;
386 bool v6, v4, bh;
d3765386 387
268c24ee
RW
388 if (nh_type) {
389 switch (nh_type) {
390 case NEXTHOP_TYPE_IPV4:
391 case NEXTHOP_TYPE_IPV4_IFINDEX:
392 return AFI_IP;
393 case NEXTHOP_TYPE_IPV6:
394 case NEXTHOP_TYPE_IPV6_IFINDEX:
395 return AFI_IP6;
396 case NEXTHOP_TYPE_IFINDEX:
397 case NEXTHOP_TYPE_BLACKHOLE:
398 return AFI_MAX;
399 }
400 }
401
e5c83d9b
DS
402 v6 = v4 = bh = false;
403
268c24ee
RW
404 for (ALL_NEXTHOPS(nhg, nexthop)) {
405 nh_type = nexthop->type;
406
407 switch (nh_type) {
408 case NEXTHOP_TYPE_IFINDEX:
409 break;
410 case NEXTHOP_TYPE_IPV4:
411 case NEXTHOP_TYPE_IPV4_IFINDEX:
412 v6 = true;
413 install_afi = AFI_IP;
414 break;
415 case NEXTHOP_TYPE_IPV6:
416 case NEXTHOP_TYPE_IPV6_IFINDEX:
417 v4 = true;
418 install_afi = AFI_IP6;
419 break;
420 case NEXTHOP_TYPE_BLACKHOLE:
421 bh = true;
e5c83d9b
DS
422 break;
423 }
424 }
425
268c24ee
RW
426 /* Interface and/or blackhole nexthops only. */
427 if (!v4 && !v6)
ff9799c3 428 install_afi = AFI_MAX;
ff9799c3 429
e5c83d9b
DS
430 if (!bh && v6 && v4)
431 DEBUGD(&pbr_dbg_nht,
5e81f5dd
DS
432 "%s: Saw both V6 and V4 nexthops...using %s", __func__,
433 afi2str(install_afi));
e5c83d9b
DS
434 if (bh && (v6 || v4))
435 DEBUGD(&pbr_dbg_nht,
436 "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.",
5e81f5dd
DS
437 __func__, v4 ? "v4" : "", (v4 && v6) ? " and " : "",
438 v6 ? "v6" : "");
e5c83d9b
DS
439
440 return install_afi;
441}
442
443static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
444 struct nexthop_group nhg)
445{
446 afi_t install_afi;
aafac994 447 enum nexthop_types_t nh_type = 0;
e5c83d9b 448
aafac994 449 install_afi = pbr_nht_which_afi(nhg, nh_type);
e5c83d9b 450
e5c83d9b
DS
451 route_add(pnhgc, nhg, install_afi);
452}
453
454static void
455pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
ff9799c3 456 struct nexthop_group nhg,
aafac994 457 enum nexthop_types_t nh_type)
e5c83d9b
DS
458{
459 afi_t install_afi;
460
aafac994 461 install_afi = pbr_nht_which_afi(nhg, nh_type);
e5c83d9b
DS
462
463 pnhgc->installed = false;
464 pnhgc->valid = false;
465 route_delete(pnhgc, install_afi);
466}
467
468void pbr_nht_change_group(const char *name)
469{
470 struct nexthop_group_cmd *nhgc;
471 struct pbr_nexthop_group_cache *pnhgc;
472 struct pbr_nexthop_group_cache find;
473 struct nexthop *nhop;
474
475 nhgc = nhgc_find(name);
476 if (!nhgc)
477 return;
478
479 memset(&find, 0, sizeof(find));
6612590d 480 snprintf(find.name, sizeof(find.name), "%s", name);
e5c83d9b
DS
481 pnhgc = hash_lookup(pbr_nhg_hash, &find);
482
483 if (!pnhgc) {
484 DEBUGD(&pbr_dbg_nht,
485 "%s: Could not find nexthop-group cache w/ name '%s'",
5e81f5dd 486 __func__, name);
e5c83d9b
DS
487 return;
488 }
489
490 for (ALL_NEXTHOPS(nhgc->nhg, nhop)) {
491 struct pbr_nexthop_cache lookup;
492 struct pbr_nexthop_cache *pnhc;
493
734bf907 494 lookup.nexthop = *nhop;
e5c83d9b
DS
495 pnhc = hash_lookup(pnhgc->nhh, &lookup);
496 if (!pnhc) {
497 pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc);
498 pnhc->parent = pnhgc;
499 }
500 }
501 pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg);
502}
503
504char *pbr_nht_nexthop_make_name(char *name, size_t l,
505 uint32_t seqno, char *buffer)
506{
507 snprintf(buffer, l, "%s%u", name, seqno);
508 return buffer;
509}
510
f143cffa
SW
511void pbr_nht_add_individual_nexthop(struct pbr_map_sequence *pbrms,
512 const struct nexthop *nhop)
e5c83d9b
DS
513{
514 struct pbr_nexthop_group_cache *pnhgc;
515 struct pbr_nexthop_group_cache find;
516 struct pbr_nexthop_cache *pnhc;
e5c83d9b 517 struct pbr_nexthop_cache lookup;
f143cffa
SW
518 struct nexthop *nh;
519 char buf[PBR_NHC_NAMELEN];
520
521 pbrms->nhg = nexthop_group_new();
522 pbrms->internal_nhg_name = XSTRDUP(
523 MTYPE_TMP,
524 pbr_nht_nexthop_make_name(pbrms->parent->name, PBR_NHC_NAMELEN,
525 pbrms->seqno, buf));
526
527 nh = nexthop_new();
528 memcpy(nh, nhop, sizeof(*nh));
529
530 nexthop_group_add_sorted(pbrms->nhg, nh);
e5c83d9b 531
e5c83d9b 532 memset(&find, 0, sizeof(find));
06210d1f 533 pbr_nht_nexthop_make_name(pbrms->parent->name, PBR_NHC_NAMELEN,
e5c83d9b 534 pbrms->seqno, find.name);
a4044dc1
QY
535
536 if (!pbr_nht_get_next_tableid(true)) {
537 zlog_warn(
538 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
15569c58 539 __func__, find.name);
a4044dc1
QY
540 return;
541 }
542
e5c83d9b
DS
543 if (!pbrms->internal_nhg_name)
544 pbrms->internal_nhg_name = XSTRDUP(MTYPE_TMP, find.name);
545
546 pnhgc = hash_get(pbr_nhg_hash, &find, pbr_nhgc_alloc);
547
734bf907 548 lookup.nexthop = *pbrms->nhg->nexthop;
e5c83d9b
DS
549 pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc);
550 pnhc->parent = pnhgc;
fcf29c69
DS
551 if (nhop->vrf_id != VRF_DEFAULT) {
552 struct vrf *vrf = vrf_lookup_by_id(nhop->vrf_id);
553
554 if (vrf)
555 strlcpy(pnhc->vrf_name, vrf->name,
556 sizeof(pnhc->vrf_name));
557 }
7cbdabff
DS
558
559 if (nhop->ifindex != 0) {
560 struct interface *ifp =
561 if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
562
563 if (ifp)
564 strlcpy(pnhc->intf_name, ifp->name,
565 sizeof(pnhc->intf_name));
566 }
e5c83d9b
DS
567 pbr_nht_install_nexthop_group(pnhgc, *pbrms->nhg);
568}
569
f143cffa 570static void pbr_nht_release_individual_nexthop(struct pbr_map_sequence *pbrms)
e5c83d9b
DS
571{
572 struct pbr_nexthop_group_cache *pnhgc;
573 struct pbr_nexthop_group_cache find;
574 struct pbr_nexthop_cache *pnhc;
575 struct pbr_nexthop_cache lup;
e5c83d9b 576 struct nexthop *nh;
aafac994 577 enum nexthop_types_t nh_type = 0;
e5c83d9b 578
e5c83d9b 579 memset(&find, 0, sizeof(find));
6612590d 580 snprintf(find.name, sizeof(find.name), "%s", pbrms->internal_nhg_name);
e5c83d9b
DS
581 pnhgc = hash_lookup(pbr_nhg_hash, &find);
582
583 nh = pbrms->nhg->nexthop;
aafac994 584 nh_type = nh->type;
734bf907 585 lup.nexthop = *nh;
e5c83d9b
DS
586 pnhc = hash_lookup(pnhgc->nhh, &lup);
587 pnhc->parent = NULL;
588 hash_release(pnhgc->nhh, pnhc);
589 pbr_nh_delete(&pnhc);
aafac994 590 pbr_nht_uninstall_nexthop_group(pnhgc, *pbrms->nhg, nh_type);
e5c83d9b
DS
591
592 hash_release(pbr_nhg_hash, pnhgc);
1f375577 593 pbr_nhgc_delete(pnhgc);
e5c83d9b 594
e5c83d9b
DS
595 nexthop_group_delete(&pbrms->nhg);
596 XFREE(MTYPE_TMP, pbrms->internal_nhg_name);
597}
598
f143cffa
SW
599void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence *pbrms)
600{
601 pbr_map_delete_nexthops(pbrms);
602
603 pbr_nht_release_individual_nexthop(pbrms);
604}
605
b13e5ad6 606struct pbr_nexthop_group_cache *pbr_nht_add_group(const char *name)
e5c83d9b
DS
607{
608 struct nexthop *nhop;
609 struct nexthop_group_cmd *nhgc;
610 struct pbr_nexthop_group_cache *pnhgc;
611 struct pbr_nexthop_group_cache lookup;
612
a4044dc1
QY
613 if (!pbr_nht_get_next_tableid(true)) {
614 zlog_warn(
615 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
5e81f5dd 616 __func__, name);
a4044dc1
QY
617 return NULL;
618 }
619
e5c83d9b
DS
620 nhgc = nhgc_find(name);
621
622 if (!nhgc) {
1d5453d6 623 DEBUGD(&pbr_dbg_nht, "%s: Could not find nhgc with name: %s",
5e81f5dd 624 __func__, name);
b13e5ad6 625 return NULL;
e5c83d9b
DS
626 }
627
6612590d 628 snprintf(lookup.name, sizeof(lookup.name), "%s", name);
e5c83d9b 629 pnhgc = hash_get(pbr_nhg_hash, &lookup, pbr_nhgc_alloc);
5e81f5dd 630 DEBUGD(&pbr_dbg_nht, "%s: Retrieved NHGC @ %p", __func__, pnhgc);
e5c83d9b
DS
631
632 for (ALL_NEXTHOPS(nhgc->nhg, nhop)) {
7fe96307 633 struct pbr_nexthop_cache lookupc;
e5c83d9b
DS
634 struct pbr_nexthop_cache *pnhc;
635
734bf907 636 lookupc.nexthop = *nhop;
7fe96307 637 pnhc = hash_lookup(pnhgc->nhh, &lookupc);
e5c83d9b 638 if (!pnhc) {
7fe96307 639 pnhc = hash_get(pnhgc->nhh, &lookupc, pbr_nh_alloc);
e5c83d9b
DS
640 pnhc->parent = pnhgc;
641 }
642 }
b13e5ad6
DS
643
644 return pnhgc;
e5c83d9b
DS
645}
646
647void pbr_nht_delete_group(const char *name)
648{
649 struct pbr_map_sequence *pbrms;
650 struct listnode *snode;
651 struct pbr_map *pbrm;
b13e5ad6
DS
652 struct pbr_nexthop_group_cache pnhgc_find;
653 struct pbr_nexthop_group_cache *pnhgc;
e5c83d9b
DS
654
655 RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
656 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, snode, pbrms)) {
657 if (pbrms->nhgrp_name
b13e5ad6 658 && strmatch(pbrms->nhgrp_name, name)) {
e5c83d9b 659 pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS;
b13e5ad6
DS
660 pbrms->nhg = NULL;
661 pbrms->internal_nhg_name = NULL;
e5c83d9b
DS
662 pbrm->valid = false;
663 }
664 }
665 }
b13e5ad6
DS
666
667 strlcpy(pnhgc_find.name, name, sizeof(pnhgc_find.name));
668 pnhgc = hash_release(pbr_nhg_hash, &pnhgc_find);
669 pbr_nhgc_delete(pnhgc);
e5c83d9b
DS
670}
671
672bool pbr_nht_nexthop_valid(struct nexthop_group *nhg)
673{
15569c58 674 DEBUGD(&pbr_dbg_nht, "%s: %p", __func__, nhg);
e5c83d9b
DS
675 return true;
676}
677
678bool pbr_nht_nexthop_group_valid(const char *name)
679{
680 struct pbr_nexthop_group_cache *pnhgc;
681 struct pbr_nexthop_group_cache lookup;
682
15569c58 683 DEBUGD(&pbr_dbg_nht, "%s: %s", __func__, name);
e5c83d9b 684
6612590d 685 snprintf(lookup.name, sizeof(lookup.name), "%s", name);
e5c83d9b
DS
686 pnhgc = hash_get(pbr_nhg_hash, &lookup, NULL);
687 if (!pnhgc)
688 return false;
1d5453d6 689 DEBUGD(&pbr_dbg_nht, "%s: %d %d", __func__, pnhgc->valid,
e5c83d9b
DS
690 pnhgc->installed);
691 if (pnhgc->valid && pnhgc->installed)
692 return true;
693
694 return false;
695}
696
697struct pbr_nht_individual {
698 struct zapi_route *nhr;
a106a408 699 struct interface *ifp;
fcf29c69
DS
700 struct pbr_vrf *pbr_vrf;
701 struct pbr_nexthop_cache *pnhc;
9d961247 702 vrf_id_t old_vrf_id;
e5c83d9b 703
4bf97ce1 704 bool valid;
fcf29c69
DS
705
706 bool nhr_matched;
e5c83d9b
DS
707};
708
8babeb1a
SW
709static bool
710pbr_nht_individual_nexthop_gw_update(struct pbr_nexthop_cache *pnhc,
c59f754d 711 struct pbr_nht_individual *pnhi)
e5c83d9b 712{
8babeb1a 713 bool is_valid = pnhc->valid;
e5c83d9b 714
89527add
DS
715 /*
716 * If we have an interface down event, let's note that
717 * it is happening and find all the nexthops that depend
718 * on that interface. As that if we have an interface
719 * flapping fast enough it means that zebra might turn
720 * those nexthop tracking events into a no-update
721 * So let's search and do the right thing on the
722 * interface event.
723 */
724 if (!pnhi->nhr && pnhi->ifp) {
89527add
DS
725 switch (pnhc->nexthop.type) {
726 case NEXTHOP_TYPE_BLACKHOLE:
e6b00e3f
SW
727 case NEXTHOP_TYPE_IPV4:
728 case NEXTHOP_TYPE_IPV6:
729 goto done;
89527add
DS
730 case NEXTHOP_TYPE_IFINDEX:
731 case NEXTHOP_TYPE_IPV4_IFINDEX:
732 case NEXTHOP_TYPE_IPV6_IFINDEX:
e6b00e3f
SW
733 if (pnhc->nexthop.ifindex == pnhi->ifp->ifindex)
734 is_valid = if_is_up(pnhi->ifp);
735 goto done;
89527add
DS
736 }
737
8babeb1a 738 goto done;
89527add 739 }
e5c83d9b 740
920bb6f7
DS
741 if (pnhi->nhr) {
742 switch (pnhi->nhr->prefix.family) {
743 case AF_INET:
744 if (pnhc->nexthop.gate.ipv4.s_addr
745 != pnhi->nhr->prefix.u.prefix4.s_addr)
746 goto done; /* Unrelated change */
747 break;
748 case AF_INET6:
749 if (memcmp(&pnhc->nexthop.gate.ipv6,
750 &pnhi->nhr->prefix.u.prefix6, 16)
751 != 0)
752 goto done; /* Unrelated change */
753 break;
754 }
8babeb1a
SW
755 }
756
c59f754d 757 pnhi->nhr_matched = true;
8babeb1a
SW
758 if (!pnhi->nhr->nexthop_num) {
759 is_valid = false;
760 goto done;
761 }
762
734bf907
DS
763 if (pnhc->nexthop.type == NEXTHOP_TYPE_IPV4_IFINDEX
764 || pnhc->nexthop.type == NEXTHOP_TYPE_IPV6_IFINDEX) {
8babeb1a
SW
765
766 /* GATEWAY_IFINDEX type shouldn't resolve to group */
767 if (pnhi->nhr->nexthop_num > 1) {
768 is_valid = false;
769 goto done;
770 }
771
772 /* If whatever we resolved to wasn't on the interface we
773 * specified. (i.e. not a connected route), its invalid.
774 */
734bf907 775 if (pnhi->nhr->nexthops[0].ifindex != pnhc->nexthop.ifindex) {
8babeb1a
SW
776 is_valid = false;
777 goto done;
778 }
779 }
780
781 is_valid = true;
782
783done:
784 pnhc->valid = is_valid;
785
786 return pnhc->valid;
787}
788
c59f754d
WC
789static bool
790pbr_nht_individual_nexthop_interface_update(struct pbr_nexthop_cache *pnhc,
791 struct pbr_nht_individual *pnhi)
8babeb1a
SW
792{
793 bool is_valid = pnhc->valid;
794
795 if (!pnhi->ifp) /* It doesn't care about non-interface updates */
796 goto done;
797
734bf907 798 if (pnhc->nexthop.ifindex
8babeb1a
SW
799 != pnhi->ifp->ifindex) /* Un-related interface */
800 goto done;
801
c59f754d 802 pnhi->nhr_matched = true;
8babeb1a
SW
803 is_valid = !!if_is_up(pnhi->ifp);
804
805done:
806 pnhc->valid = is_valid;
807
808 return pnhc->valid;
809}
810
811/* Given this update either from interface or nexthop tracking, re-validate this
812 * nexthop.
813 *
814 * If the update is un-related, the subroutines shoud just return their cached
815 * valid state.
816 */
c59f754d
WC
817static void pbr_nht_individual_nexthop_update(struct pbr_nexthop_cache *pnhc,
818 struct pbr_nht_individual *pnhi)
8babeb1a
SW
819{
820 assert(pnhi->nhr || pnhi->ifp); /* Either nexthop or interface update */
821
734bf907 822 switch (pnhc->nexthop.type) {
8babeb1a
SW
823 case NEXTHOP_TYPE_IFINDEX:
824 pbr_nht_individual_nexthop_interface_update(pnhc, pnhi);
825 break;
cb254f41 826 case NEXTHOP_TYPE_IPV6_IFINDEX:
734bf907 827 if (IN6_IS_ADDR_LINKLOCAL(&pnhc->nexthop.gate.ipv6)) {
cb254f41
SW
828 pbr_nht_individual_nexthop_interface_update(pnhc, pnhi);
829 break;
830 }
831 /* Intentional fall thru */
832 case NEXTHOP_TYPE_IPV4_IFINDEX:
8babeb1a
SW
833 case NEXTHOP_TYPE_IPV4:
834 case NEXTHOP_TYPE_IPV6:
8babeb1a
SW
835 pbr_nht_individual_nexthop_gw_update(pnhc, pnhi);
836 break;
837 case NEXTHOP_TYPE_BLACKHOLE:
838 pnhc->valid = true;
e5c83d9b
DS
839 break;
840 }
8babeb1a
SW
841}
842
843static void pbr_nht_individual_nexthop_update_lookup(struct hash_bucket *b,
844 void *data)
845{
846 struct pbr_nexthop_cache *pnhc = b->data;
847 struct pbr_nht_individual *pnhi = data;
8babeb1a
SW
848 bool old_valid;
849
850 old_valid = pnhc->valid;
851
852 pbr_nht_individual_nexthop_update(pnhc, pnhi);
e5c83d9b 853
1d5453d6 854 DEBUGD(&pbr_dbg_nht, " Found %pFX: old: %d new: %d",
2dbe669b 855 &pnhi->nhr->prefix, old_valid, pnhc->valid);
e5c83d9b 856
e5c83d9b 857 if (pnhc->valid)
4bf97ce1 858 pnhi->valid = true;
e5c83d9b
DS
859}
860
b822b93a
SW
861static void pbr_nexthop_group_cache_iterate_to_group(struct hash_bucket *b,
862 void *data)
863{
864 struct pbr_nexthop_cache *pnhc = b->data;
865 struct nexthop_group *nhg = data;
866 struct nexthop *nh = NULL;
867
734bf907 868 copy_nexthops(&nh, &pnhc->nexthop, NULL);
b822b93a 869
50d89650 870 _nexthop_add(&nhg->nexthop, nh);
b822b93a
SW
871}
872
873static void
874pbr_nexthop_group_cache_to_nexthop_group(struct nexthop_group *nhg,
875 struct pbr_nexthop_group_cache *pnhgc)
876{
877 hash_iterate(pnhgc->nhh, pbr_nexthop_group_cache_iterate_to_group, nhg);
878}
879
e3b78da8 880static void pbr_nht_nexthop_update_lookup(struct hash_bucket *b, void *data)
e5c83d9b
DS
881{
882 struct pbr_nexthop_group_cache *pnhgc = b->data;
8babeb1a 883 struct pbr_nht_individual pnhi = {};
b822b93a 884 struct nexthop_group nhg = {};
b13e5ad6
DS
885 bool old_valid;
886
887 old_valid = pnhgc->valid;
e5c83d9b
DS
888
889 pnhi.nhr = (struct zapi_route *)data;
4bf97ce1 890 pnhi.valid = false;
c59f754d 891 pnhi.nhr_matched = false;
e5c83d9b
DS
892 hash_iterate(pnhgc->nhh, pbr_nht_individual_nexthop_update_lookup,
893 &pnhi);
894
c59f754d
WC
895 if (!pnhi.nhr_matched)
896 return;
897
e5c83d9b
DS
898 /*
899 * If any of the specified nexthops are valid we are valid
900 */
901 pnhgc->valid = !!pnhi.valid;
b13e5ad6 902
6db1188f
SW
903 pbr_nexthop_group_cache_to_nexthop_group(&nhg, pnhgc);
904
905 if (pnhgc->valid)
b822b93a 906 pbr_nht_install_nexthop_group(pnhgc, nhg);
6db1188f
SW
907 else
908 pbr_nht_uninstall_nexthop_group(pnhgc, nhg, 0);
909
910 /* Don't need copied nexthops anymore */
911 nexthops_free(nhg.nexthop);
b822b93a 912
b13e5ad6
DS
913 if (old_valid != pnhgc->valid)
914 pbr_map_check_nh_group_change(pnhgc->name);
e5c83d9b
DS
915}
916
917void pbr_nht_nexthop_update(struct zapi_route *nhr)
918{
919 hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_update_lookup, nhr);
920}
921
9d961247
DS
922struct nhrc_vrf_info {
923 struct pbr_vrf *pbr_vrf;
924 uint32_t old_vrf_id;
925 struct nhrc *nhrc;
926};
927
928static int pbr_nht_nhrc_vrf_change(struct hash_bucket *b, void *data)
929{
930 struct nhrc *nhrc = b->data;
931 struct nhrc_vrf_info *nhrcvi = data;
932
933 if (nhrc->nexthop.vrf_id == nhrcvi->old_vrf_id) {
934 nhrcvi->nhrc = nhrc;
935 return HASHWALK_ABORT;
936 }
937
938 return HASHWALK_CONTINUE;
939}
940
941static int pbr_nht_individual_nexthop_vrf_handle(struct hash_bucket *b,
942 void *data)
fcf29c69
DS
943{
944 struct pbr_nexthop_cache *pnhc = b->data;
945 struct pbr_nht_individual *pnhi = data;
946
9d961247
DS
947 if (pnhc->looked_at == true)
948 return HASHWALK_CONTINUE;
949
734bf907 950 if (pnhc->nexthop.vrf_id == VRF_DEFAULT)
9d961247 951 return HASHWALK_CONTINUE;
fcf29c69 952
9d961247
DS
953 if (strncmp(pnhc->vrf_name, pbr_vrf_name(pnhi->pbr_vrf),
954 sizeof(pnhc->vrf_name))
955 == 0) {
fcf29c69 956 pnhi->pnhc = pnhc;
9d961247 957
734bf907 958 if (pnhc->nexthop.vrf_id != pbr_vrf_id(pnhi->pbr_vrf)) {
9d961247
DS
959 struct nhrc_vrf_info nhrcvi;
960
961 memset(&nhrcvi, 0, sizeof(nhrcvi));
962 nhrcvi.pbr_vrf = pnhi->pbr_vrf;
734bf907 963 nhrcvi.old_vrf_id = pnhc->nexthop.vrf_id;
9d961247
DS
964
965 pnhi->nhr_matched = true;
734bf907 966 pnhi->old_vrf_id = pnhc->nexthop.vrf_id;
9d961247
DS
967
968 do {
969 nhrcvi.nhrc = NULL;
970 hash_walk(pbr_nhrc_hash,
971 pbr_nht_nhrc_vrf_change, &nhrcvi);
972 if (nhrcvi.nhrc) {
973 hash_release(pbr_nhrc_hash,
974 nhrcvi.nhrc);
975 nhrcvi.nhrc->nexthop.vrf_id =
976 pbr_vrf_id(pnhi->pbr_vrf);
977 hash_get(pbr_nhrc_hash, nhrcvi.nhrc,
978 hash_alloc_intern);
979 pbr_send_rnh(&nhrcvi.nhrc->nexthop, true);
980 }
981 } while (nhrcvi.nhrc);
982 }
983
984 pnhc->looked_at = true;
985 return HASHWALK_ABORT;
fcf29c69 986 }
9d961247
DS
987
988 return HASHWALK_CONTINUE;
989}
990
991static void pbr_nht_clear_looked_at(struct hash_bucket *b, void *data)
992{
993 struct pbr_nexthop_cache *pnhc = b->data;
994
995 pnhc->looked_at = false;
fcf29c69
DS
996}
997
998static void pbr_nht_nexthop_vrf_handle(struct hash_bucket *b, void *data)
999{
1000 struct pbr_nexthop_group_cache *pnhgc = b->data;
1001 struct pbr_vrf *pbr_vrf = data;
1002 struct pbr_nht_individual pnhi = {};
fcf29c69 1003
9d961247
DS
1004 hash_iterate(pnhgc->nhh, pbr_nht_clear_looked_at, NULL);
1005 memset(&pnhi, 0, sizeof(pnhi));
1006 pnhi.pbr_vrf = pbr_vrf;
fcf29c69 1007 do {
9d961247
DS
1008 struct pbr_nexthop_cache *pnhc;
1009
1010 pnhi.pnhc = NULL;
1011 hash_walk(pnhgc->nhh, pbr_nht_individual_nexthop_vrf_handle,
1012 &pnhi);
fcf29c69
DS
1013
1014 if (!pnhi.pnhc)
1015 continue;
1016
9d961247 1017 pnhc = pnhi.pnhc;
734bf907 1018 pnhc->nexthop.vrf_id = pnhi.old_vrf_id;
fcf29c69 1019 pnhi.pnhc = hash_release(pnhgc->nhh, pnhi.pnhc);
9d961247 1020 if (pnhi.pnhc) {
734bf907 1021 pnhi.pnhc->nexthop.vrf_id = pbr_vrf_id(pbr_vrf);
fcf29c69 1022
9d961247
DS
1023 hash_get(pnhgc->nhh, pnhi.pnhc, hash_alloc_intern);
1024 } else
734bf907 1025 pnhc->nexthop.vrf_id = pbr_vrf_id(pbr_vrf);
fcf29c69
DS
1026
1027 pbr_map_check_vrf_nh_group_change(pnhgc->name, pbr_vrf,
9d961247 1028 pnhi.old_vrf_id);
fcf29c69
DS
1029 } while (pnhi.pnhc);
1030}
1031
1032void pbr_nht_vrf_update(struct pbr_vrf *pbr_vrf)
1033{
1034 hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_vrf_handle, pbr_vrf);
1035}
1036
7cbdabff
DS
1037static void pbr_nht_individual_nexthop_interface_handle(struct hash_bucket *b,
1038 void *data)
1039{
1040 struct pbr_nexthop_cache *pnhc = b->data;
1041 struct pbr_nht_individual *pnhi = data;
1042
734bf907 1043 if (pnhc->nexthop.ifindex == 0)
7cbdabff
DS
1044 return;
1045
1046 if ((strncmp(pnhc->intf_name, pnhi->ifp->name, sizeof(pnhc->intf_name))
1047 == 0)
734bf907 1048 && pnhc->nexthop.ifindex != pnhi->ifp->ifindex)
7cbdabff
DS
1049 pnhi->pnhc = pnhc;
1050}
1051
1052static void pbr_nht_nexthop_interface_handle(struct hash_bucket *b, void *data)
1053{
1054 struct pbr_nexthop_group_cache *pnhgc = b->data;
1055 struct interface *ifp = data;
1056 struct pbr_nht_individual pnhi = {};
1057 struct nhrc *nhrc;
1058 uint32_t old_ifindex;
1059
1060 do {
1061 memset(&pnhi, 0, sizeof(pnhi));
1062 pnhi.ifp = ifp;
1063 hash_iterate(pnhgc->nhh,
1064 pbr_nht_individual_nexthop_interface_handle,
1065 &pnhi);
1066
1067 if (!pnhi.pnhc)
1068 continue;
1069
1070 pnhi.pnhc = hash_release(pnhgc->nhh, pnhi.pnhc);
734bf907 1071 old_ifindex = pnhi.pnhc->nexthop.ifindex;
7cbdabff 1072
734bf907 1073 nhrc = hash_lookup(pbr_nhrc_hash, &pnhi.pnhc->nexthop);
7cbdabff
DS
1074 if (nhrc) {
1075 hash_release(pbr_nhrc_hash, nhrc);
1076 nhrc->nexthop.ifindex = ifp->ifindex;
1077 hash_get(pbr_nhrc_hash, nhrc, hash_alloc_intern);
1078 }
734bf907 1079 pnhi.pnhc->nexthop.ifindex = ifp->ifindex;
7cbdabff
DS
1080
1081 hash_get(pnhgc->nhh, pnhi.pnhc, hash_alloc_intern);
1082
1083 pbr_map_check_interface_nh_group_change(pnhgc->name, ifp,
1084 old_ifindex);
1085 } while (pnhi.pnhc);
1086}
1087
1088void pbr_nht_interface_update(struct interface *ifp)
1089{
1090 hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_interface_handle, ifp);
1091}
1092
a106a408 1093static void
7f5818fb 1094pbr_nht_individual_nexthop_interface_update_lookup(struct hash_bucket *b,
a106a408
RW
1095 void *data)
1096{
1097 struct pbr_nexthop_cache *pnhc = b->data;
1098 struct pbr_nht_individual *pnhi = data;
1099 bool old_valid;
1100
1101 old_valid = pnhc->valid;
1102
8babeb1a 1103 pbr_nht_individual_nexthop_update(pnhc, pnhi);
a106a408 1104
1d5453d6 1105 DEBUGD(&pbr_dbg_nht, " Found %s: old: %d new: %d", pnhi->ifp->name,
a106a408
RW
1106 old_valid, pnhc->valid);
1107
1108 if (pnhc->valid)
4bf97ce1 1109 pnhi->valid = true;
a106a408
RW
1110}
1111
7f5818fb 1112static void pbr_nht_nexthop_interface_update_lookup(struct hash_bucket *b,
a106a408
RW
1113 void *data)
1114{
1115 struct pbr_nexthop_group_cache *pnhgc = b->data;
8babeb1a 1116 struct pbr_nht_individual pnhi = {};
89527add 1117 struct nexthop_group nhg = {};
a106a408
RW
1118 bool old_valid;
1119
1120 old_valid = pnhgc->valid;
1121
1122 pnhi.ifp = data;
4bf97ce1 1123 pnhi.valid = false;
a106a408
RW
1124 hash_iterate(pnhgc->nhh,
1125 pbr_nht_individual_nexthop_interface_update_lookup, &pnhi);
1126
1127 /*
1128 * If any of the specified nexthops are valid we are valid
1129 */
4bf97ce1 1130 pnhgc->valid = pnhi.valid;
a106a408 1131
89527add
DS
1132 pbr_nexthop_group_cache_to_nexthop_group(&nhg, pnhgc);
1133
1134 if (pnhgc->valid)
1135 pbr_nht_install_nexthop_group(pnhgc, nhg);
1136 else
1137 pbr_nht_uninstall_nexthop_group(pnhgc, nhg, 0);
1138
1139 nexthops_free(nhg.nexthop);
1140
a106a408
RW
1141 if (old_valid != pnhgc->valid)
1142 pbr_map_check_nh_group_change(pnhgc->name);
1143}
1144
1145void pbr_nht_nexthop_interface_update(struct interface *ifp)
1146{
1147 hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_interface_update_lookup,
1148 ifp);
1149}
1150
d8b87afe 1151static uint32_t pbr_nhg_hash_key(const void *arg)
e5c83d9b 1152{
d8b87afe 1153 const struct pbr_nexthop_group_cache *nhgc = arg;
e5c83d9b
DS
1154
1155 return jhash(&nhgc->name, strlen(nhgc->name), 0x52c34a96);
1156}
1157
74df8d6d 1158static bool pbr_nhg_hash_equal(const void *arg1, const void *arg2)
e5c83d9b
DS
1159{
1160 const struct pbr_nexthop_group_cache *nhgc1 =
1161 (const struct pbr_nexthop_group_cache *)arg1;
1162 const struct pbr_nexthop_group_cache *nhgc2 =
1163 (const struct pbr_nexthop_group_cache *)arg2;
1164
1165 return !strcmp(nhgc1->name, nhgc2->name);
1166}
1167
a4044dc1 1168uint32_t pbr_nht_get_next_tableid(bool peek)
e5c83d9b
DS
1169{
1170 uint32_t i;
1171 bool found = false;
1172
1173 for (i = pbr_nhg_low_table; i <= pbr_nhg_high_table; i++) {
d8729f8c 1174 if (!nhg_tableid[i]) {
e5c83d9b
DS
1175 found = true;
1176 break;
1177 }
1178 }
1179
1180 if (found) {
a4044dc1 1181 nhg_tableid[i] = !peek;
e5c83d9b
DS
1182 return i;
1183 } else
1184 return 0;
1185}
1186
1187void pbr_nht_set_tableid_range(uint32_t low, uint32_t high)
1188{
1189 pbr_nhg_low_table = low;
1190 pbr_nhg_high_table = high;
1191}
1192
1193void pbr_nht_write_table_range(struct vty *vty)
1194{
1195 if (pbr_nhg_low_table != PBR_NHT_DEFAULT_LOW_TABLEID
1196 || pbr_nhg_high_table != PBR_NHT_DEFAULT_HIGH_TABLEID) {
1197 vty_out(vty, "pbr table range %u %u\n", pbr_nhg_low_table,
1198 pbr_nhg_high_table);
1199 }
1200}
1201
1202uint32_t pbr_nht_get_next_rule(uint32_t seqno)
1203{
1204 return seqno + pbr_nhg_low_rule - 1;
1205}
1206void pbr_nht_set_rule_range(uint32_t low, uint32_t high)
1207{
1208 pbr_nhg_low_rule = low;
1209 pbr_nhg_high_rule = high;
1210}
1211
1212void pbr_nht_write_rule_range(struct vty *vty)
1213{
1214 if (pbr_nhg_low_rule != PBR_NHT_DEFAULT_LOW_RULE
1215 || pbr_nhg_high_rule != PBR_NHT_DEFAULT_HIGH_RULE) {
1216 vty_out(vty, "pbr rule range %u %u\n", pbr_nhg_low_rule,
1217 pbr_nhg_high_rule);
1218 }
1219}
1220
1221uint32_t pbr_nht_get_table(const char *name)
1222{
1223 struct pbr_nexthop_group_cache find;
1224 struct pbr_nexthop_group_cache *pnhgc;
1225
1226 memset(&find, 0, sizeof(find));
6612590d 1227 snprintf(find.name, sizeof(find.name), "%s", name);
e5c83d9b
DS
1228 pnhgc = hash_lookup(pbr_nhg_hash, &find);
1229
1230 if (!pnhgc) {
1231 DEBUGD(&pbr_dbg_nht,
1232 "%s: Could not find nexthop-group cache w/ name '%s'",
15569c58 1233 __func__, name);
e5c83d9b
DS
1234 return 5000;
1235 }
1236
1237 return pnhgc->table_id;
1238}
1239
1240bool pbr_nht_get_installed(const char *name)
1241{
1242 struct pbr_nexthop_group_cache find;
1243 struct pbr_nexthop_group_cache *pnhgc;
1244
1245 memset(&find, 0, sizeof(find));
6612590d 1246 snprintf(find.name, sizeof(find.name), "%s", name);
e5c83d9b
DS
1247
1248 pnhgc = hash_lookup(pbr_nhg_hash, &find);
1249
d3765386 1250 if (!pnhgc)
e5c83d9b 1251 return false;
e5c83d9b
DS
1252
1253 return pnhgc->installed;
1254}
1255
e3b78da8 1256static void pbr_nht_show_nhg_nexthops(struct hash_bucket *b, void *data)
e5c83d9b
DS
1257{
1258 struct pbr_nexthop_cache *pnhc = b->data;
1259 struct vty *vty = data;
1260
57cdafc4 1261 vty_out(vty, "\tValid: %d ", pnhc->valid);
734bf907 1262 nexthop_group_write_nexthop(vty, &pnhc->nexthop);
e5c83d9b
DS
1263}
1264
010dd8ed
WC
1265static void pbr_nht_json_nhg_nexthops(struct hash_bucket *b, void *data)
1266{
1267 struct pbr_nexthop_cache *pnhc = b->data;
1268 json_object *all_hops = data;
1269 json_object *this_hop;
1270
1271 this_hop = json_object_new_object();
734bf907 1272 nexthop_group_json_nexthop(this_hop, &pnhc->nexthop);
3e81618d 1273 json_object_boolean_add(this_hop, "valid", pnhc->valid);
010dd8ed
WC
1274
1275 json_object_array_add(all_hops, this_hop);
1276}
1277
e5c83d9b
DS
1278struct pbr_nht_show {
1279 struct vty *vty;
010dd8ed 1280 json_object *json;
e5c83d9b
DS
1281 const char *name;
1282};
1283
e3b78da8 1284static void pbr_nht_show_nhg(struct hash_bucket *b, void *data)
e5c83d9b
DS
1285{
1286 struct pbr_nexthop_group_cache *pnhgc = b->data;
1287 struct pbr_nht_show *pns = data;
1288 struct vty *vty;
1289
1290 if (pns->name && strcmp(pns->name, pnhgc->name) != 0)
1291 return;
1292
1293 vty = pns->vty;
1294 vty_out(vty, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n",
1295 pnhgc->name, pnhgc->table_id, pnhgc->valid, pnhgc->installed);
1296
1297 hash_iterate(pnhgc->nhh, pbr_nht_show_nhg_nexthops, vty);
1298}
1299
010dd8ed
WC
1300static void pbr_nht_json_nhg(struct hash_bucket *b, void *data)
1301{
1302 struct pbr_nexthop_group_cache *pnhgc = b->data;
1303 struct pbr_nht_show *pns = data;
1304 json_object *j, *this_group, *group_hops;
1305
1306 if (pns->name && strcmp(pns->name, pnhgc->name) != 0)
1307 return;
1308
1309 j = pns->json;
1310 this_group = json_object_new_object();
1311
1312 if (!j || !this_group)
1313 return;
1314
010dd8ed 1315 json_object_int_add(this_group, "id", pnhgc->table_id);
81c0078e 1316 json_object_string_add(this_group, "name", pnhgc->name);
3e81618d
WC
1317 json_object_boolean_add(this_group, "valid", pnhgc->valid);
1318 json_object_boolean_add(this_group, "installed", pnhgc->installed);
010dd8ed
WC
1319
1320 group_hops = json_object_new_array();
1321
1322 if (group_hops) {
1323 hash_iterate(pnhgc->nhh, pbr_nht_json_nhg_nexthops, group_hops);
1324 json_object_object_add(this_group, "nexthops", group_hops);
1325 }
1326
dadba1a2 1327 json_object_array_add(j, this_group);
010dd8ed
WC
1328}
1329
e5c83d9b
DS
1330void pbr_nht_show_nexthop_group(struct vty *vty, const char *name)
1331{
1332 struct pbr_nht_show pns;
1333
1334 pns.vty = vty;
1335 pns.name = name;
1336
1337 hash_iterate(pbr_nhg_hash, pbr_nht_show_nhg, &pns);
1338}
1339
010dd8ed
WC
1340void pbr_nht_json_nexthop_group(json_object *j, const char *name)
1341{
1342 struct pbr_nht_show pns;
1343
1344 pns.name = name;
1345 pns.json = j;
1346
1347 hash_iterate(pbr_nhg_hash, pbr_nht_json_nhg, &pns);
1348}
1349
e5c83d9b
DS
1350void pbr_nht_init(void)
1351{
1352 pbr_nhg_hash = hash_create_size(
1353 16, pbr_nhg_hash_key, pbr_nhg_hash_equal, "PBR NHG Cache Hash");
b13e5ad6 1354 pbr_nhrc_hash =
d8b87afe 1355 hash_create_size(16, (unsigned int (*)(const void *))nexthop_hash,
b13e5ad6 1356 pbr_nhrc_hash_equal, "PBR NH Hash");
e5c83d9b
DS
1357
1358 pbr_nhg_low_table = PBR_NHT_DEFAULT_LOW_TABLEID;
1359 pbr_nhg_high_table = PBR_NHT_DEFAULT_HIGH_TABLEID;
1360 pbr_nhg_low_rule = PBR_NHT_DEFAULT_LOW_RULE;
1361 pbr_nhg_high_rule = PBR_NHT_DEFAULT_HIGH_RULE;
1362 memset(&nhg_tableid, 0, 65535 * sizeof(uint8_t));
1363}