3 * Copyright (C) 2018 Cumulus Networks, Inc.
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
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.
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
24 #include "nexthop_group.h"
25 #include "nexthop_group_private.h"
32 #include "pbrd/pbr_nht.h"
33 #include "pbrd/pbr_map.h"
34 #include "pbrd/pbr_zebra.h"
35 #include "pbrd/pbr_memory.h"
36 #include "pbrd/pbr_debug.h"
38 DEFINE_MTYPE_STATIC(PBRD
, PBR_NHG
, "PBR Nexthop Groups")
40 static struct hash
*pbr_nhg_hash
;
41 static struct hash
*pbr_nhrc_hash
;
43 static uint32_t pbr_nhg_low_table
;
44 static uint32_t pbr_nhg_high_table
;
45 static uint32_t pbr_nhg_low_rule
;
46 static uint32_t pbr_nhg_high_rule
;
47 static bool nhg_tableid
[65535];
49 static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
50 struct nexthop_group nhg
);
52 pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
53 struct nexthop_group nhg
,
54 enum nexthop_types_t nh_type
);
60 struct nexthop nexthop
;
61 unsigned int refcount
;
64 /* Hash functions for pbr_nhrc_hash ---------------------------------------- */
66 static void *pbr_nhrc_hash_alloc(void *p
)
68 struct nhrc
*nhrc
= XCALLOC(MTYPE_PBR_NHG
, sizeof(struct nhrc
));
69 nhrc
->nexthop
= *(struct nexthop
*)p
;
70 nhrc
->nexthop
.next
= NULL
;
71 nhrc
->nexthop
.prev
= NULL
;
75 static bool pbr_nhrc_hash_equal(const void *arg1
, const void *arg2
)
77 const struct nexthop
*nh1
, *nh2
;
82 return nexthop_same(nh1
, nh2
);
85 /* ------------------------------------------------------------------------- */
87 static void *pbr_nh_alloc(void *p
)
89 struct pbr_nexthop_cache
*new;
90 struct pbr_nexthop_cache
*pnhc
= (struct pbr_nexthop_cache
*)p
;
93 new = XCALLOC(MTYPE_PBR_NHG
, sizeof(*new));
94 nhrc
= hash_get(pbr_nhrc_hash
, pnhc
->nexthop
, pbr_nhrc_hash_alloc
);
95 new->nexthop
= &nhrc
->nexthop
;
97 /* Decremented again in pbr_nh_delete */
100 DEBUGD(&pbr_dbg_nht
, "%s: Sending nexthop to Zebra", __func__
);
102 pbr_send_rnh(new->nexthop
, true);
108 static void pbr_nh_delete(struct pbr_nexthop_cache
**pnhc
)
112 nhrc
= hash_lookup(pbr_nhrc_hash
, (*pnhc
)->nexthop
);
116 if (!nhrc
|| nhrc
->refcount
== 0) {
117 DEBUGD(&pbr_dbg_nht
, "%s: Removing nexthop from Zebra",
119 pbr_send_rnh((*pnhc
)->nexthop
, false);
121 if (nhrc
&& nhrc
->refcount
== 0) {
122 hash_release(pbr_nhrc_hash
, nhrc
);
123 XFREE(MTYPE_PBR_NHG
, nhrc
);
126 XFREE(MTYPE_PBR_NHG
, *pnhc
);
129 static void pbr_nh_delete_iterate(struct hash_bucket
*b
, void *p
)
131 pbr_nh_delete((struct pbr_nexthop_cache
**)&b
->data
);
134 static uint32_t pbr_nh_hash_key(const void *arg
)
137 const struct pbr_nexthop_cache
*pbrnc
= arg
;
139 key
= nexthop_hash(pbrnc
->nexthop
);
144 static bool pbr_nh_hash_equal(const void *arg1
, const void *arg2
)
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
;
151 if (pbrnc1
->nexthop
->vrf_id
!= pbrnc2
->nexthop
->vrf_id
)
154 if (pbrnc1
->nexthop
->ifindex
!= pbrnc2
->nexthop
->ifindex
)
157 if (pbrnc1
->nexthop
->type
!= pbrnc2
->nexthop
->type
)
160 switch (pbrnc1
->nexthop
->type
) {
161 case NEXTHOP_TYPE_IFINDEX
:
162 return pbrnc1
->nexthop
->ifindex
== pbrnc2
->nexthop
->ifindex
;
163 case NEXTHOP_TYPE_IPV4_IFINDEX
:
164 case NEXTHOP_TYPE_IPV4
:
165 return pbrnc1
->nexthop
->gate
.ipv4
.s_addr
166 == pbrnc2
->nexthop
->gate
.ipv4
.s_addr
;
167 case NEXTHOP_TYPE_IPV6_IFINDEX
:
168 case NEXTHOP_TYPE_IPV6
:
169 return !memcmp(&pbrnc1
->nexthop
->gate
.ipv6
,
170 &pbrnc2
->nexthop
->gate
.ipv6
, 16);
171 case NEXTHOP_TYPE_BLACKHOLE
:
172 return pbrnc1
->nexthop
->bh_type
== pbrnc2
->nexthop
->bh_type
;
176 * We should not get here
181 static void pbr_nhgc_delete(struct pbr_nexthop_group_cache
*p
)
183 hash_iterate(p
->nhh
, pbr_nh_delete_iterate
, NULL
);
185 XFREE(MTYPE_PBR_NHG
, p
);
188 static void *pbr_nhgc_alloc(void *p
)
190 struct pbr_nexthop_group_cache
*new;
191 struct pbr_nexthop_group_cache
*pnhgc
=
192 (struct pbr_nexthop_group_cache
*)p
;
194 new = XCALLOC(MTYPE_PBR_NHG
, sizeof(*new));
196 strlcpy(new->name
, pnhgc
->name
, sizeof(pnhgc
->name
));
197 new->table_id
= pbr_nht_get_next_tableid(false);
199 DEBUGD(&pbr_dbg_nht
, "%s: NHT: %s assigned Table ID: %u", __func__
,
200 new->name
, new->table_id
);
202 new->nhh
= hash_create_size(8, pbr_nh_hash_key
, pbr_nh_hash_equal
,
203 "PBR NH Cache Hash");
208 void pbr_nhgroup_add_cb(const char *name
)
210 struct pbr_nexthop_group_cache
*pnhgc
;
211 struct nexthop_group_cmd
*nhgc
;
213 nhgc
= nhgc_find(name
);
216 DEBUGD(&pbr_dbg_nht
, "%s: Could not find nhgc with name: %s\n",
221 pnhgc
= pbr_nht_add_group(name
);
226 DEBUGD(&pbr_dbg_nht
, "%s: Added nexthop-group %s", __func__
, name
);
228 pbr_map_check_nh_group_change(name
);
231 void pbr_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
232 const struct nexthop
*nhop
)
235 struct pbr_nexthop_group_cache pnhgc_find
= {};
236 struct pbr_nexthop_group_cache
*pnhgc
;
237 struct pbr_nexthop_cache pnhc_find
= {};
238 struct pbr_nexthop_cache
*pnhc
;
240 if (!pbr_nht_get_next_tableid(true)) {
242 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
243 __func__
, nhgc
->name
);
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
);
251 /* create & insert new pnhc into pnhgc->nhh */
252 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
253 pnhc
= hash_get(pnhgc
->nhh
, &pnhc_find
, pbr_nh_alloc
);
254 pnhc_find
.nexthop
= NULL
;
256 /* set parent pnhgc */
257 pnhc
->parent
= pnhgc
;
259 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
260 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
261 DEBUGD(&pbr_dbg_nht
, "%s: Added %s to nexthop-group %s",
262 __func__
, debugstr
, nhgc
->name
);
265 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
266 pbr_map_check_nh_group_change(nhgc
->name
);
268 if (nhop
->type
== NEXTHOP_TYPE_IFINDEX
269 || (nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
270 && IN6_IS_ADDR_LINKLOCAL(&nhop
->gate
.ipv6
))) {
271 struct interface
*ifp
;
273 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
275 pbr_nht_nexthop_interface_update(ifp
);
279 void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
280 const struct nexthop
*nhop
)
283 struct pbr_nexthop_group_cache pnhgc_find
= {};
284 struct pbr_nexthop_group_cache
*pnhgc
;
285 struct pbr_nexthop_cache pnhc_find
= {};
286 struct pbr_nexthop_cache
*pnhc
;
287 enum nexthop_types_t nh_type
= nhop
->type
;
289 /* find pnhgc by name */
290 strlcpy(pnhgc_find
.name
, nhgc
->name
, sizeof(pnhgc_find
.name
));
291 pnhgc
= hash_lookup(pbr_nhg_hash
, &pnhgc_find
);
293 /* delete pnhc from pnhgc->nhh */
294 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
295 pnhc
= hash_release(pnhgc
->nhh
, &pnhc_find
);
298 pbr_nh_delete(&pnhc
);
300 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
301 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
302 DEBUGD(&pbr_dbg_nht
, "%s: Removed %s from nexthop-group %s",
303 __func__
, debugstr
, nhgc
->name
);
306 if (pnhgc
->nhh
->count
)
307 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
309 pbr_nht_uninstall_nexthop_group(pnhgc
, nhgc
->nhg
, nh_type
);
311 pbr_map_check_nh_group_change(nhgc
->name
);
314 void pbr_nhgroup_delete_cb(const char *name
)
316 DEBUGD(&pbr_dbg_nht
, "%s: Removed nexthop-group %s", __func__
, name
);
318 /* delete group from all pbrms's */
319 pbr_nht_delete_group(name
);
321 pbr_map_check_nh_group_change(name
);
325 static struct pbr_nexthop_cache
*pbr_nht_lookup_nexthop(struct nexthop
*nexthop
)
331 static void pbr_nht_find_nhg_from_table_install(struct hash_bucket
*b
,
334 struct pbr_nexthop_group_cache
*pnhgc
=
335 (struct pbr_nexthop_group_cache
*)b
->data
;
336 uint32_t *table_id
= (uint32_t *)data
;
338 if (pnhgc
->table_id
== *table_id
) {
339 DEBUGD(&pbr_dbg_nht
, "%s: Table ID (%u) matches %s", __func__
,
340 *table_id
, pnhgc
->name
);
343 * If the table has been re-handled by zebra
344 * and we are already installed no need to do
347 if (!pnhgc
->installed
) {
348 pnhgc
->installed
= true;
349 pbr_map_schedule_policy_from_nhg(pnhgc
->name
);
354 void pbr_nht_route_installed_for_table(uint32_t table_id
)
356 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_install
,
360 static void pbr_nht_find_nhg_from_table_remove(struct hash_bucket
*b
,
366 void pbr_nht_route_removed_for_table(uint32_t table_id
)
368 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_remove
,
373 * Loop through all nexthops in a nexthop group to check that they are all the
374 * same. If they are not all the same, log this peculiarity.
377 * The nexthop group to check
380 * - AFI of last nexthop in the group
383 static afi_t
pbr_nht_which_afi(struct nexthop_group nhg
,
384 enum nexthop_types_t nh_type
)
386 struct nexthop
*nexthop
;
387 afi_t install_afi
= AFI_MAX
;
392 case NEXTHOP_TYPE_IPV4
:
393 case NEXTHOP_TYPE_IPV4_IFINDEX
:
395 case NEXTHOP_TYPE_IPV6
:
396 case NEXTHOP_TYPE_IPV6_IFINDEX
:
398 case NEXTHOP_TYPE_IFINDEX
:
399 case NEXTHOP_TYPE_BLACKHOLE
:
404 v6
= v4
= bh
= false;
406 for (ALL_NEXTHOPS(nhg
, nexthop
)) {
407 nh_type
= nexthop
->type
;
410 case NEXTHOP_TYPE_IFINDEX
:
412 case NEXTHOP_TYPE_IPV4
:
413 case NEXTHOP_TYPE_IPV4_IFINDEX
:
415 install_afi
= AFI_IP
;
417 case NEXTHOP_TYPE_IPV6
:
418 case NEXTHOP_TYPE_IPV6_IFINDEX
:
420 install_afi
= AFI_IP6
;
422 case NEXTHOP_TYPE_BLACKHOLE
:
428 /* Interface and/or blackhole nexthops only. */
430 install_afi
= AFI_MAX
;
434 "%s: Saw both V6 and V4 nexthops...using %s", __func__
,
435 afi2str(install_afi
));
436 if (bh
&& (v6
|| v4
))
438 "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.",
439 __func__
, v4
? "v4" : "", (v4
&& v6
) ? " and " : "",
445 static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
446 struct nexthop_group nhg
)
449 enum nexthop_types_t nh_type
= 0;
451 install_afi
= pbr_nht_which_afi(nhg
, nh_type
);
453 route_add(pnhgc
, nhg
, install_afi
);
457 pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
458 struct nexthop_group nhg
,
459 enum nexthop_types_t nh_type
)
463 install_afi
= pbr_nht_which_afi(nhg
, nh_type
);
465 pnhgc
->installed
= false;
466 pnhgc
->valid
= false;
467 route_delete(pnhgc
, install_afi
);
470 void pbr_nht_change_group(const char *name
)
472 struct nexthop_group_cmd
*nhgc
;
473 struct pbr_nexthop_group_cache
*pnhgc
;
474 struct pbr_nexthop_group_cache find
;
475 struct nexthop
*nhop
;
477 nhgc
= nhgc_find(name
);
481 memset(&find
, 0, sizeof(find
));
482 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
483 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
487 "%s: Could not find nexthop-group cache w/ name '%s'",
492 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
493 struct pbr_nexthop_cache lookup
;
494 struct pbr_nexthop_cache
*pnhc
;
496 lookup
.nexthop
= nhop
;
497 pnhc
= hash_lookup(pnhgc
->nhh
, &lookup
);
499 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
500 pnhc
->parent
= pnhgc
;
503 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
506 char *pbr_nht_nexthop_make_name(char *name
, size_t l
,
507 uint32_t seqno
, char *buffer
)
509 snprintf(buffer
, l
, "%s%u", name
, seqno
);
513 void pbr_nht_add_individual_nexthop(struct pbr_map_sequence
*pbrms
)
515 struct pbr_nexthop_group_cache
*pnhgc
;
516 struct pbr_nexthop_group_cache find
;
517 struct pbr_nexthop_cache
*pnhc
;
518 struct pbr_nexthop_cache lookup
;
520 memset(&find
, 0, sizeof(find
));
521 pbr_nht_nexthop_make_name(pbrms
->parent
->name
, PBR_NHC_NAMELEN
,
522 pbrms
->seqno
, find
.name
);
524 if (!pbr_nht_get_next_tableid(true)) {
526 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
527 __func__
, find
.name
);
531 if (!pbrms
->internal_nhg_name
)
532 pbrms
->internal_nhg_name
= XSTRDUP(MTYPE_TMP
, find
.name
);
534 pnhgc
= hash_get(pbr_nhg_hash
, &find
, pbr_nhgc_alloc
);
536 lookup
.nexthop
= pbrms
->nhg
->nexthop
;
537 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
538 pnhc
->parent
= pnhgc
;
539 pbr_nht_install_nexthop_group(pnhgc
, *pbrms
->nhg
);
542 void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence
*pbrms
)
544 struct pbr_nexthop_group_cache
*pnhgc
;
545 struct pbr_nexthop_group_cache find
;
546 struct pbr_nexthop_cache
*pnhc
;
547 struct pbr_nexthop_cache lup
;
549 enum nexthop_types_t nh_type
= 0;
551 pbr_map_delete_nexthops(pbrms
);
553 memset(&find
, 0, sizeof(find
));
554 snprintf(find
.name
, sizeof(find
.name
), "%s", pbrms
->internal_nhg_name
);
555 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
557 nh
= pbrms
->nhg
->nexthop
;
560 pnhc
= hash_lookup(pnhgc
->nhh
, &lup
);
562 hash_release(pnhgc
->nhh
, pnhc
);
563 pbr_nh_delete(&pnhc
);
564 pbr_nht_uninstall_nexthop_group(pnhgc
, *pbrms
->nhg
, nh_type
);
566 hash_release(pbr_nhg_hash
, pnhgc
);
568 nexthop_group_delete(&pbrms
->nhg
);
569 XFREE(MTYPE_TMP
, pbrms
->internal_nhg_name
);
572 struct pbr_nexthop_group_cache
*pbr_nht_add_group(const char *name
)
574 struct nexthop
*nhop
;
575 struct nexthop_group_cmd
*nhgc
;
576 struct pbr_nexthop_group_cache
*pnhgc
;
577 struct pbr_nexthop_group_cache lookup
;
579 if (!pbr_nht_get_next_tableid(true)) {
581 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
586 nhgc
= nhgc_find(name
);
589 DEBUGD(&pbr_dbg_nht
, "%s: Could not find nhgc with name: %s\n",
594 snprintf(lookup
.name
, sizeof(lookup
.name
), "%s", name
);
595 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, pbr_nhgc_alloc
);
596 DEBUGD(&pbr_dbg_nht
, "%s: Retrieved NHGC @ %p", __func__
, pnhgc
);
598 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
599 struct pbr_nexthop_cache lookupc
;
600 struct pbr_nexthop_cache
*pnhc
;
602 lookupc
.nexthop
= nhop
;
603 pnhc
= hash_lookup(pnhgc
->nhh
, &lookupc
);
605 pnhc
= hash_get(pnhgc
->nhh
, &lookupc
, pbr_nh_alloc
);
606 pnhc
->parent
= pnhgc
;
613 void pbr_nht_delete_group(const char *name
)
615 struct pbr_map_sequence
*pbrms
;
616 struct listnode
*snode
;
617 struct pbr_map
*pbrm
;
618 struct pbr_nexthop_group_cache pnhgc_find
;
619 struct pbr_nexthop_group_cache
*pnhgc
;
621 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
622 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, snode
, pbrms
)) {
623 if (pbrms
->nhgrp_name
624 && strmatch(pbrms
->nhgrp_name
, name
)) {
625 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
627 pbrms
->internal_nhg_name
= NULL
;
633 strlcpy(pnhgc_find
.name
, name
, sizeof(pnhgc_find
.name
));
634 pnhgc
= hash_release(pbr_nhg_hash
, &pnhgc_find
);
635 pbr_nhgc_delete(pnhgc
);
638 bool pbr_nht_nexthop_valid(struct nexthop_group
*nhg
)
640 DEBUGD(&pbr_dbg_nht
, "%s: %p", __func__
, nhg
);
644 bool pbr_nht_nexthop_group_valid(const char *name
)
646 struct pbr_nexthop_group_cache
*pnhgc
;
647 struct pbr_nexthop_group_cache lookup
;
649 DEBUGD(&pbr_dbg_nht
, "%s: %s", __func__
, name
);
651 snprintf(lookup
.name
, sizeof(lookup
.name
), "%s", name
);
652 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, NULL
);
655 DEBUGD(&pbr_dbg_nht
, "%s: \t%d %d", __func__
, pnhgc
->valid
,
657 if (pnhgc
->valid
&& pnhgc
->installed
)
663 struct pbr_nht_individual
{
664 struct zapi_route
*nhr
;
665 struct interface
*ifp
;
671 pbr_nht_individual_nexthop_gw_update(struct pbr_nexthop_cache
*pnhc
,
672 const struct pbr_nht_individual
*pnhi
)
674 bool is_valid
= pnhc
->valid
;
676 if (!pnhi
->nhr
) /* It doesn't care about non-nexthop updates */
679 switch (pnhi
->nhr
->prefix
.family
) {
681 if (pnhc
->nexthop
->gate
.ipv4
.s_addr
682 != pnhi
->nhr
->prefix
.u
.prefix4
.s_addr
)
683 goto done
; /* Unrelated change */
686 if (memcmp(&pnhc
->nexthop
->gate
.ipv6
,
687 &pnhi
->nhr
->prefix
.u
.prefix6
, 16)
689 goto done
; /* Unrelated change */
693 if (!pnhi
->nhr
->nexthop_num
) {
698 if (pnhc
->nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
699 || pnhc
->nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
701 /* GATEWAY_IFINDEX type shouldn't resolve to group */
702 if (pnhi
->nhr
->nexthop_num
> 1) {
707 /* If whatever we resolved to wasn't on the interface we
708 * specified. (i.e. not a connected route), its invalid.
710 if (pnhi
->nhr
->nexthops
[0].ifindex
!= pnhc
->nexthop
->ifindex
) {
719 pnhc
->valid
= is_valid
;
724 static bool pbr_nht_individual_nexthop_interface_update(
725 struct pbr_nexthop_cache
*pnhc
, const struct pbr_nht_individual
*pnhi
)
727 bool is_valid
= pnhc
->valid
;
729 if (!pnhi
->ifp
) /* It doesn't care about non-interface updates */
732 if (pnhc
->nexthop
->ifindex
733 != pnhi
->ifp
->ifindex
) /* Un-related interface */
736 is_valid
= !!if_is_up(pnhi
->ifp
);
739 pnhc
->valid
= is_valid
;
744 /* Given this update either from interface or nexthop tracking, re-validate this
747 * If the update is un-related, the subroutines shoud just return their cached
751 pbr_nht_individual_nexthop_update(struct pbr_nexthop_cache
*pnhc
,
752 const struct pbr_nht_individual
*pnhi
)
754 assert(pnhi
->nhr
|| pnhi
->ifp
); /* Either nexthop or interface update */
756 switch (pnhc
->nexthop
->type
) {
757 case NEXTHOP_TYPE_IFINDEX
:
758 pbr_nht_individual_nexthop_interface_update(pnhc
, pnhi
);
760 case NEXTHOP_TYPE_IPV6_IFINDEX
:
761 if (IN6_IS_ADDR_LINKLOCAL(&pnhc
->nexthop
->gate
.ipv6
)) {
762 pbr_nht_individual_nexthop_interface_update(pnhc
, pnhi
);
765 /* Intentional fall thru */
766 case NEXTHOP_TYPE_IPV4_IFINDEX
:
767 case NEXTHOP_TYPE_IPV4
:
768 case NEXTHOP_TYPE_IPV6
:
769 pbr_nht_individual_nexthop_gw_update(pnhc
, pnhi
);
771 case NEXTHOP_TYPE_BLACKHOLE
:
777 static void pbr_nht_individual_nexthop_update_lookup(struct hash_bucket
*b
,
780 struct pbr_nexthop_cache
*pnhc
= b
->data
;
781 struct pbr_nht_individual
*pnhi
= data
;
782 char buf
[PREFIX_STRLEN
];
785 old_valid
= pnhc
->valid
;
787 pbr_nht_individual_nexthop_update(pnhc
, pnhi
);
789 DEBUGD(&pbr_dbg_nht
, "\tFound %s: old: %d new: %d",
790 prefix2str(&pnhi
->nhr
->prefix
, buf
, sizeof(buf
)), old_valid
,
797 static void pbr_nexthop_group_cache_iterate_to_group(struct hash_bucket
*b
,
800 struct pbr_nexthop_cache
*pnhc
= b
->data
;
801 struct nexthop_group
*nhg
= data
;
802 struct nexthop
*nh
= NULL
;
804 copy_nexthops(&nh
, pnhc
->nexthop
, NULL
);
806 _nexthop_add(&nhg
->nexthop
, nh
);
810 pbr_nexthop_group_cache_to_nexthop_group(struct nexthop_group
*nhg
,
811 struct pbr_nexthop_group_cache
*pnhgc
)
813 hash_iterate(pnhgc
->nhh
, pbr_nexthop_group_cache_iterate_to_group
, nhg
);
816 static void pbr_nht_nexthop_update_lookup(struct hash_bucket
*b
, void *data
)
818 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
819 struct pbr_nht_individual pnhi
= {};
820 struct nexthop_group nhg
= {};
823 old_valid
= pnhgc
->valid
;
825 pnhi
.nhr
= (struct zapi_route
*)data
;
827 hash_iterate(pnhgc
->nhh
, pbr_nht_individual_nexthop_update_lookup
,
831 * If any of the specified nexthops are valid we are valid
833 pnhgc
->valid
= !!pnhi
.valid
;
836 pbr_nexthop_group_cache_to_nexthop_group(&nhg
, pnhgc
);
837 pbr_nht_install_nexthop_group(pnhgc
, nhg
);
838 /* Don't need copied nexthops anymore */
839 nexthops_free(nhg
.nexthop
);
842 if (old_valid
!= pnhgc
->valid
)
843 pbr_map_check_nh_group_change(pnhgc
->name
);
846 void pbr_nht_nexthop_update(struct zapi_route
*nhr
)
848 hash_iterate(pbr_nhg_hash
, pbr_nht_nexthop_update_lookup
, nhr
);
852 pbr_nht_individual_nexthop_interface_update_lookup(struct hash_bucket
*b
,
855 struct pbr_nexthop_cache
*pnhc
= b
->data
;
856 struct pbr_nht_individual
*pnhi
= data
;
859 old_valid
= pnhc
->valid
;
861 pbr_nht_individual_nexthop_update(pnhc
, pnhi
);
863 DEBUGD(&pbr_dbg_nht
, "\tFound %s: old: %d new: %d", pnhi
->ifp
->name
,
864 old_valid
, pnhc
->valid
);
870 static void pbr_nht_nexthop_interface_update_lookup(struct hash_bucket
*b
,
873 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
874 struct pbr_nht_individual pnhi
= {};
877 old_valid
= pnhgc
->valid
;
881 hash_iterate(pnhgc
->nhh
,
882 pbr_nht_individual_nexthop_interface_update_lookup
, &pnhi
);
885 * If any of the specified nexthops are valid we are valid
887 pnhgc
->valid
= !!pnhi
.valid
;
889 if (old_valid
!= pnhgc
->valid
)
890 pbr_map_check_nh_group_change(pnhgc
->name
);
893 void pbr_nht_nexthop_interface_update(struct interface
*ifp
)
895 hash_iterate(pbr_nhg_hash
, pbr_nht_nexthop_interface_update_lookup
,
899 static uint32_t pbr_nhg_hash_key(const void *arg
)
901 const struct pbr_nexthop_group_cache
*nhgc
= arg
;
903 return jhash(&nhgc
->name
, strlen(nhgc
->name
), 0x52c34a96);
906 static bool pbr_nhg_hash_equal(const void *arg1
, const void *arg2
)
908 const struct pbr_nexthop_group_cache
*nhgc1
=
909 (const struct pbr_nexthop_group_cache
*)arg1
;
910 const struct pbr_nexthop_group_cache
*nhgc2
=
911 (const struct pbr_nexthop_group_cache
*)arg2
;
913 return !strcmp(nhgc1
->name
, nhgc2
->name
);
916 uint32_t pbr_nht_get_next_tableid(bool peek
)
921 for (i
= pbr_nhg_low_table
; i
<= pbr_nhg_high_table
; i
++) {
922 if (!nhg_tableid
[i
]) {
929 nhg_tableid
[i
] = !peek
;
935 void pbr_nht_set_tableid_range(uint32_t low
, uint32_t high
)
937 pbr_nhg_low_table
= low
;
938 pbr_nhg_high_table
= high
;
941 void pbr_nht_write_table_range(struct vty
*vty
)
943 if (pbr_nhg_low_table
!= PBR_NHT_DEFAULT_LOW_TABLEID
944 || pbr_nhg_high_table
!= PBR_NHT_DEFAULT_HIGH_TABLEID
) {
945 vty_out(vty
, "pbr table range %u %u\n", pbr_nhg_low_table
,
950 uint32_t pbr_nht_get_next_rule(uint32_t seqno
)
952 return seqno
+ pbr_nhg_low_rule
- 1;
954 void pbr_nht_set_rule_range(uint32_t low
, uint32_t high
)
956 pbr_nhg_low_rule
= low
;
957 pbr_nhg_high_rule
= high
;
960 void pbr_nht_write_rule_range(struct vty
*vty
)
962 if (pbr_nhg_low_rule
!= PBR_NHT_DEFAULT_LOW_RULE
963 || pbr_nhg_high_rule
!= PBR_NHT_DEFAULT_HIGH_RULE
) {
964 vty_out(vty
, "pbr rule range %u %u\n", pbr_nhg_low_rule
,
969 uint32_t pbr_nht_get_table(const char *name
)
971 struct pbr_nexthop_group_cache find
;
972 struct pbr_nexthop_group_cache
*pnhgc
;
974 memset(&find
, 0, sizeof(find
));
975 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
976 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
980 "%s: Could not find nexthop-group cache w/ name '%s'",
985 return pnhgc
->table_id
;
988 bool pbr_nht_get_installed(const char *name
)
990 struct pbr_nexthop_group_cache find
;
991 struct pbr_nexthop_group_cache
*pnhgc
;
993 memset(&find
, 0, sizeof(find
));
994 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
996 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
1001 return pnhgc
->installed
;
1004 static void pbr_nht_show_nhg_nexthops(struct hash_bucket
*b
, void *data
)
1006 struct pbr_nexthop_cache
*pnhc
= b
->data
;
1007 struct vty
*vty
= data
;
1009 vty_out(vty
, "\tValid: %d ", pnhc
->valid
);
1010 nexthop_group_write_nexthop(vty
, pnhc
->nexthop
);
1013 struct pbr_nht_show
{
1018 static void pbr_nht_show_nhg(struct hash_bucket
*b
, void *data
)
1020 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
1021 struct pbr_nht_show
*pns
= data
;
1024 if (pns
->name
&& strcmp(pns
->name
, pnhgc
->name
) != 0)
1028 vty_out(vty
, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n",
1029 pnhgc
->name
, pnhgc
->table_id
, pnhgc
->valid
, pnhgc
->installed
);
1031 hash_iterate(pnhgc
->nhh
, pbr_nht_show_nhg_nexthops
, vty
);
1034 void pbr_nht_show_nexthop_group(struct vty
*vty
, const char *name
)
1036 struct pbr_nht_show pns
;
1041 hash_iterate(pbr_nhg_hash
, pbr_nht_show_nhg
, &pns
);
1044 void pbr_nht_init(void)
1046 pbr_nhg_hash
= hash_create_size(
1047 16, pbr_nhg_hash_key
, pbr_nhg_hash_equal
, "PBR NHG Cache Hash");
1049 hash_create_size(16, (unsigned int (*)(const void *))nexthop_hash
,
1050 pbr_nhrc_hash_equal
, "PBR NH Hash");
1052 pbr_nhg_low_table
= PBR_NHT_DEFAULT_LOW_TABLEID
;
1053 pbr_nhg_high_table
= PBR_NHT_DEFAULT_HIGH_TABLEID
;
1054 pbr_nhg_low_rule
= PBR_NHT_DEFAULT_LOW_RULE
;
1055 pbr_nhg_high_rule
= PBR_NHT_DEFAULT_HIGH_RULE
;
1056 memset(&nhg_tableid
, 0, 65535 * sizeof(uint8_t));