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",
101 __PRETTY_FUNCTION__
);
103 pbr_send_rnh(new->nexthop
, true);
109 static void pbr_nh_delete(struct pbr_nexthop_cache
**pnhc
)
113 nhrc
= hash_lookup(pbr_nhrc_hash
, (*pnhc
)->nexthop
);
117 if (!nhrc
|| nhrc
->refcount
== 0) {
118 DEBUGD(&pbr_dbg_nht
, "%s: Removing nexthop from Zebra",
119 __PRETTY_FUNCTION__
);
120 pbr_send_rnh((*pnhc
)->nexthop
, false);
122 if (nhrc
&& nhrc
->refcount
== 0) {
123 hash_release(pbr_nhrc_hash
, nhrc
);
124 XFREE(MTYPE_PBR_NHG
, nhrc
);
127 XFREE(MTYPE_PBR_NHG
, *pnhc
);
130 static void pbr_nh_delete_iterate(struct hash_bucket
*b
, void *p
)
132 pbr_nh_delete((struct pbr_nexthop_cache
**)&b
->data
);
135 static uint32_t pbr_nh_hash_key(const void *arg
)
138 const struct pbr_nexthop_cache
*pbrnc
= arg
;
140 key
= nexthop_hash(pbrnc
->nexthop
);
145 static bool pbr_nh_hash_equal(const void *arg1
, const void *arg2
)
147 const struct pbr_nexthop_cache
*pbrnc1
=
148 (const struct pbr_nexthop_cache
*)arg1
;
149 const struct pbr_nexthop_cache
*pbrnc2
=
150 (const struct pbr_nexthop_cache
*)arg2
;
152 if (pbrnc1
->nexthop
->vrf_id
!= pbrnc2
->nexthop
->vrf_id
)
155 if (pbrnc1
->nexthop
->ifindex
!= pbrnc2
->nexthop
->ifindex
)
158 if (pbrnc1
->nexthop
->type
!= pbrnc2
->nexthop
->type
)
161 switch (pbrnc1
->nexthop
->type
) {
162 case NEXTHOP_TYPE_IFINDEX
:
163 return pbrnc1
->nexthop
->ifindex
== pbrnc2
->nexthop
->ifindex
;
164 case NEXTHOP_TYPE_IPV4_IFINDEX
:
165 case NEXTHOP_TYPE_IPV4
:
166 return pbrnc1
->nexthop
->gate
.ipv4
.s_addr
167 == pbrnc2
->nexthop
->gate
.ipv4
.s_addr
;
168 case NEXTHOP_TYPE_IPV6_IFINDEX
:
169 case NEXTHOP_TYPE_IPV6
:
170 return !memcmp(&pbrnc1
->nexthop
->gate
.ipv6
,
171 &pbrnc2
->nexthop
->gate
.ipv6
, 16);
172 case NEXTHOP_TYPE_BLACKHOLE
:
173 return pbrnc1
->nexthop
->bh_type
== pbrnc2
->nexthop
->bh_type
;
177 * We should not get here
182 static void pbr_nhgc_delete(struct pbr_nexthop_group_cache
*p
)
184 hash_iterate(p
->nhh
, pbr_nh_delete_iterate
, NULL
);
186 XFREE(MTYPE_PBR_NHG
, p
);
189 static void *pbr_nhgc_alloc(void *p
)
191 struct pbr_nexthop_group_cache
*new;
192 struct pbr_nexthop_group_cache
*pnhgc
=
193 (struct pbr_nexthop_group_cache
*)p
;
195 new = XCALLOC(MTYPE_PBR_NHG
, sizeof(*new));
197 strlcpy(new->name
, pnhgc
->name
, sizeof(pnhgc
->name
));
198 new->table_id
= pbr_nht_get_next_tableid(false);
200 DEBUGD(&pbr_dbg_nht
, "%s: NHT: %s assigned Table ID: %u",
201 __PRETTY_FUNCTION__
, new->name
, new->table_id
);
203 new->nhh
= hash_create_size(8, pbr_nh_hash_key
, pbr_nh_hash_equal
,
204 "PBR NH Cache Hash");
209 void pbr_nhgroup_add_cb(const char *name
)
211 struct pbr_nexthop_group_cache
*pnhgc
;
212 struct nexthop_group_cmd
*nhgc
;
214 nhgc
= nhgc_find(name
);
217 DEBUGD(&pbr_dbg_nht
, "%s: Could not find nhgc with name: %s\n",
218 __PRETTY_FUNCTION__
, name
);
222 pnhgc
= pbr_nht_add_group(name
);
227 DEBUGD(&pbr_dbg_nht
, "%s: Added nexthop-group %s", __PRETTY_FUNCTION__
,
230 pbr_map_check_nh_group_change(name
);
233 void pbr_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
234 const struct nexthop
*nhop
)
237 struct pbr_nexthop_group_cache pnhgc_find
= {};
238 struct pbr_nexthop_group_cache
*pnhgc
;
239 struct pbr_nexthop_cache pnhc_find
= {};
240 struct pbr_nexthop_cache
*pnhc
;
242 if (!pbr_nht_get_next_tableid(true)) {
244 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
245 __PRETTY_FUNCTION__
, nhgc
->name
);
249 /* find pnhgc by name */
250 strlcpy(pnhgc_find
.name
, nhgc
->name
, sizeof(pnhgc_find
.name
));
251 pnhgc
= hash_get(pbr_nhg_hash
, &pnhgc_find
, pbr_nhgc_alloc
);
253 /* create & insert new pnhc into pnhgc->nhh */
254 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
255 pnhc
= hash_get(pnhgc
->nhh
, &pnhc_find
, pbr_nh_alloc
);
256 pnhc_find
.nexthop
= NULL
;
258 /* set parent pnhgc */
259 pnhc
->parent
= pnhgc
;
261 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
262 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
263 DEBUGD(&pbr_dbg_nht
, "%s: Added %s to nexthop-group %s",
264 __PRETTY_FUNCTION__
, debugstr
, nhgc
->name
);
267 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
268 pbr_map_check_nh_group_change(nhgc
->name
);
270 if (nhop
->type
== NEXTHOP_TYPE_IFINDEX
271 || (nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
272 && IN6_IS_ADDR_LINKLOCAL(&nhop
->gate
.ipv6
))) {
273 struct interface
*ifp
;
275 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
277 pbr_nht_nexthop_interface_update(ifp
);
281 void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
282 const struct nexthop
*nhop
)
285 struct pbr_nexthop_group_cache pnhgc_find
= {};
286 struct pbr_nexthop_group_cache
*pnhgc
;
287 struct pbr_nexthop_cache pnhc_find
= {};
288 struct pbr_nexthop_cache
*pnhc
;
289 enum nexthop_types_t nh_type
= nhop
->type
;
291 /* find pnhgc by name */
292 strlcpy(pnhgc_find
.name
, nhgc
->name
, sizeof(pnhgc_find
.name
));
293 pnhgc
= hash_lookup(pbr_nhg_hash
, &pnhgc_find
);
295 /* delete pnhc from pnhgc->nhh */
296 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
297 pnhc
= hash_release(pnhgc
->nhh
, &pnhc_find
);
300 pbr_nh_delete(&pnhc
);
302 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
303 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
304 DEBUGD(&pbr_dbg_nht
, "%s: Removed %s from nexthop-group %s",
305 __PRETTY_FUNCTION__
, debugstr
, nhgc
->name
);
308 if (pnhgc
->nhh
->count
)
309 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
311 pbr_nht_uninstall_nexthop_group(pnhgc
, nhgc
->nhg
, nh_type
);
313 pbr_map_check_nh_group_change(nhgc
->name
);
316 void pbr_nhgroup_delete_cb(const char *name
)
318 DEBUGD(&pbr_dbg_nht
, "%s: Removed nexthop-group %s",
319 __PRETTY_FUNCTION__
, name
);
321 /* delete group from all pbrms's */
322 pbr_nht_delete_group(name
);
324 pbr_map_check_nh_group_change(name
);
328 static struct pbr_nexthop_cache
*pbr_nht_lookup_nexthop(struct nexthop
*nexthop
)
334 static void pbr_nht_find_nhg_from_table_install(struct hash_bucket
*b
,
337 struct pbr_nexthop_group_cache
*pnhgc
=
338 (struct pbr_nexthop_group_cache
*)b
->data
;
339 uint32_t *table_id
= (uint32_t *)data
;
341 if (pnhgc
->table_id
== *table_id
) {
342 DEBUGD(&pbr_dbg_nht
, "%s: Table ID (%u) matches %s",
343 __PRETTY_FUNCTION__
, *table_id
, pnhgc
->name
);
346 * If the table has been re-handled by zebra
347 * and we are already installed no need to do
350 if (!pnhgc
->installed
) {
351 pnhgc
->installed
= true;
352 pbr_map_schedule_policy_from_nhg(pnhgc
->name
);
357 void pbr_nht_route_installed_for_table(uint32_t table_id
)
359 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_install
,
363 static void pbr_nht_find_nhg_from_table_remove(struct hash_bucket
*b
,
369 void pbr_nht_route_removed_for_table(uint32_t table_id
)
371 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_remove
,
376 * Loop through all nexthops in a nexthop group to check that they are all the
377 * same. If they are not all the same, log this peculiarity.
380 * The nexthop group to check
383 * - AFI of last nexthop in the group
386 static afi_t
pbr_nht_which_afi(struct nexthop_group nhg
,
387 enum nexthop_types_t nh_type
)
389 struct nexthop
*nexthop
;
390 afi_t install_afi
= AFI_MAX
;
395 case NEXTHOP_TYPE_IPV4
:
396 case NEXTHOP_TYPE_IPV4_IFINDEX
:
398 case NEXTHOP_TYPE_IPV6
:
399 case NEXTHOP_TYPE_IPV6_IFINDEX
:
401 case NEXTHOP_TYPE_IFINDEX
:
402 case NEXTHOP_TYPE_BLACKHOLE
:
407 v6
= v4
= bh
= false;
409 for (ALL_NEXTHOPS(nhg
, nexthop
)) {
410 nh_type
= nexthop
->type
;
413 case NEXTHOP_TYPE_IFINDEX
:
415 case NEXTHOP_TYPE_IPV4
:
416 case NEXTHOP_TYPE_IPV4_IFINDEX
:
418 install_afi
= AFI_IP
;
420 case NEXTHOP_TYPE_IPV6
:
421 case NEXTHOP_TYPE_IPV6_IFINDEX
:
423 install_afi
= AFI_IP6
;
425 case NEXTHOP_TYPE_BLACKHOLE
:
431 /* Interface and/or blackhole nexthops only. */
433 install_afi
= AFI_MAX
;
437 "%s: Saw both V6 and V4 nexthops...using %s",
438 __PRETTY_FUNCTION__
, afi2str(install_afi
));
439 if (bh
&& (v6
|| v4
))
441 "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.",
442 __PRETTY_FUNCTION__
, v4
? "v4" : "",
443 (v4
&& v6
) ? " and " : "", v6
? "v6" : "");
448 static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
449 struct nexthop_group nhg
)
452 enum nexthop_types_t nh_type
= 0;
454 install_afi
= pbr_nht_which_afi(nhg
, nh_type
);
456 route_add(pnhgc
, nhg
, install_afi
);
460 pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
461 struct nexthop_group nhg
,
462 enum nexthop_types_t nh_type
)
466 install_afi
= pbr_nht_which_afi(nhg
, nh_type
);
468 pnhgc
->installed
= false;
469 pnhgc
->valid
= false;
470 route_delete(pnhgc
, install_afi
);
473 void pbr_nht_change_group(const char *name
)
475 struct nexthop_group_cmd
*nhgc
;
476 struct pbr_nexthop_group_cache
*pnhgc
;
477 struct pbr_nexthop_group_cache find
;
478 struct nexthop
*nhop
;
480 nhgc
= nhgc_find(name
);
484 memset(&find
, 0, sizeof(find
));
485 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
486 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
490 "%s: Could not find nexthop-group cache w/ name '%s'",
491 __PRETTY_FUNCTION__
, name
);
495 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
496 struct pbr_nexthop_cache lookup
;
497 struct pbr_nexthop_cache
*pnhc
;
499 lookup
.nexthop
= nhop
;
500 pnhc
= hash_lookup(pnhgc
->nhh
, &lookup
);
502 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
503 pnhc
->parent
= pnhgc
;
506 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
509 char *pbr_nht_nexthop_make_name(char *name
, size_t l
,
510 uint32_t seqno
, char *buffer
)
512 snprintf(buffer
, l
, "%s%u", name
, seqno
);
516 void pbr_nht_add_individual_nexthop(struct pbr_map_sequence
*pbrms
)
518 struct pbr_nexthop_group_cache
*pnhgc
;
519 struct pbr_nexthop_group_cache find
;
520 struct pbr_nexthop_cache
*pnhc
;
521 struct pbr_nexthop_cache lookup
;
523 memset(&find
, 0, sizeof(find
));
524 pbr_nht_nexthop_make_name(pbrms
->parent
->name
, PBR_NHC_NAMELEN
,
525 pbrms
->seqno
, find
.name
);
527 if (!pbr_nht_get_next_tableid(true)) {
529 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
530 __PRETTY_FUNCTION__
, find
.name
);
534 if (!pbrms
->internal_nhg_name
)
535 pbrms
->internal_nhg_name
= XSTRDUP(MTYPE_TMP
, find
.name
);
537 pnhgc
= hash_get(pbr_nhg_hash
, &find
, pbr_nhgc_alloc
);
539 lookup
.nexthop
= pbrms
->nhg
->nexthop
;
540 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
541 pnhc
->parent
= pnhgc
;
542 pbr_nht_install_nexthop_group(pnhgc
, *pbrms
->nhg
);
545 void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence
*pbrms
)
547 struct pbr_nexthop_group_cache
*pnhgc
;
548 struct pbr_nexthop_group_cache find
;
549 struct pbr_nexthop_cache
*pnhc
;
550 struct pbr_nexthop_cache lup
;
551 struct pbr_map
*pbrm
= pbrms
->parent
;
552 struct listnode
*node
;
553 struct pbr_map_interface
*pmi
;
555 enum nexthop_types_t nh_type
= 0;
557 if (pbrm
->valid
&& pbrms
->nhs_installed
&& pbrm
->incoming
->count
) {
558 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, node
, pmi
))
559 pbr_send_pbr_map(pbrms
, pmi
, false);
563 pbrms
->nhs_installed
= false;
564 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
566 memset(&find
, 0, sizeof(find
));
567 snprintf(find
.name
, sizeof(find
.name
), "%s", pbrms
->internal_nhg_name
);
568 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
570 nh
= pbrms
->nhg
->nexthop
;
573 pnhc
= hash_lookup(pnhgc
->nhh
, &lup
);
575 hash_release(pnhgc
->nhh
, pnhc
);
576 pbr_nh_delete(&pnhc
);
577 pbr_nht_uninstall_nexthop_group(pnhgc
, *pbrms
->nhg
, nh_type
);
579 hash_release(pbr_nhg_hash
, pnhgc
);
581 _nexthop_del(pbrms
->nhg
, nh
);
583 nexthop_group_delete(&pbrms
->nhg
);
584 XFREE(MTYPE_TMP
, pbrms
->internal_nhg_name
);
587 struct pbr_nexthop_group_cache
*pbr_nht_add_group(const char *name
)
589 struct nexthop
*nhop
;
590 struct nexthop_group_cmd
*nhgc
;
591 struct pbr_nexthop_group_cache
*pnhgc
;
592 struct pbr_nexthop_group_cache lookup
;
594 if (!pbr_nht_get_next_tableid(true)) {
596 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
597 __PRETTY_FUNCTION__
, name
);
601 nhgc
= nhgc_find(name
);
604 DEBUGD(&pbr_dbg_nht
, "%s: Could not find nhgc with name: %s\n",
605 __PRETTY_FUNCTION__
, name
);
609 snprintf(lookup
.name
, sizeof(lookup
.name
), "%s", name
);
610 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, pbr_nhgc_alloc
);
611 DEBUGD(&pbr_dbg_nht
, "%s: Retrieved NHGC @ %p", __PRETTY_FUNCTION__
,
614 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
615 struct pbr_nexthop_cache lookupc
;
616 struct pbr_nexthop_cache
*pnhc
;
618 lookupc
.nexthop
= nhop
;
619 pnhc
= hash_lookup(pnhgc
->nhh
, &lookupc
);
621 pnhc
= hash_get(pnhgc
->nhh
, &lookupc
, pbr_nh_alloc
);
622 pnhc
->parent
= pnhgc
;
629 void pbr_nht_delete_group(const char *name
)
631 struct pbr_map_sequence
*pbrms
;
632 struct listnode
*snode
;
633 struct pbr_map
*pbrm
;
634 struct pbr_nexthop_group_cache pnhgc_find
;
635 struct pbr_nexthop_group_cache
*pnhgc
;
637 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
638 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, snode
, pbrms
)) {
639 if (pbrms
->nhgrp_name
640 && strmatch(pbrms
->nhgrp_name
, name
)) {
641 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
643 pbrms
->internal_nhg_name
= NULL
;
649 strlcpy(pnhgc_find
.name
, name
, sizeof(pnhgc_find
.name
));
650 pnhgc
= hash_release(pbr_nhg_hash
, &pnhgc_find
);
651 pbr_nhgc_delete(pnhgc
);
654 bool pbr_nht_nexthop_valid(struct nexthop_group
*nhg
)
656 DEBUGD(&pbr_dbg_nht
, "%s: %p", __PRETTY_FUNCTION__
, nhg
);
660 bool pbr_nht_nexthop_group_valid(const char *name
)
662 struct pbr_nexthop_group_cache
*pnhgc
;
663 struct pbr_nexthop_group_cache lookup
;
665 DEBUGD(&pbr_dbg_nht
, "%s: %s", __PRETTY_FUNCTION__
, name
);
667 snprintf(lookup
.name
, sizeof(lookup
.name
), "%s", name
);
668 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, NULL
);
671 DEBUGD(&pbr_dbg_nht
, "%s: \t%d %d", __PRETTY_FUNCTION__
, pnhgc
->valid
,
673 if (pnhgc
->valid
&& pnhgc
->installed
)
679 struct pbr_nht_individual
{
680 struct zapi_route
*nhr
;
681 struct interface
*ifp
;
687 pbr_nht_individual_nexthop_gw_update(struct pbr_nexthop_cache
*pnhc
,
688 const struct pbr_nht_individual
*pnhi
)
690 bool is_valid
= pnhc
->valid
;
692 if (!pnhi
->nhr
) /* It doesn't care about non-nexthop updates */
695 switch (pnhi
->nhr
->prefix
.family
) {
697 if (pnhc
->nexthop
->gate
.ipv4
.s_addr
698 != pnhi
->nhr
->prefix
.u
.prefix4
.s_addr
)
699 goto done
; /* Unrelated change */
702 if (memcmp(&pnhc
->nexthop
->gate
.ipv6
,
703 &pnhi
->nhr
->prefix
.u
.prefix6
, 16)
705 goto done
; /* Unrelated change */
709 if (!pnhi
->nhr
->nexthop_num
) {
714 if (pnhc
->nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
715 || pnhc
->nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
717 /* GATEWAY_IFINDEX type shouldn't resolve to group */
718 if (pnhi
->nhr
->nexthop_num
> 1) {
723 /* If whatever we resolved to wasn't on the interface we
724 * specified. (i.e. not a connected route), its invalid.
726 if (pnhi
->nhr
->nexthops
[0].ifindex
!= pnhc
->nexthop
->ifindex
) {
735 pnhc
->valid
= is_valid
;
740 static bool pbr_nht_individual_nexthop_interface_update(
741 struct pbr_nexthop_cache
*pnhc
, const struct pbr_nht_individual
*pnhi
)
743 bool is_valid
= pnhc
->valid
;
745 if (!pnhi
->ifp
) /* It doesn't care about non-interface updates */
748 if (pnhc
->nexthop
->ifindex
749 != pnhi
->ifp
->ifindex
) /* Un-related interface */
752 is_valid
= !!if_is_up(pnhi
->ifp
);
755 pnhc
->valid
= is_valid
;
760 /* Given this update either from interface or nexthop tracking, re-validate this
763 * If the update is un-related, the subroutines shoud just return their cached
767 pbr_nht_individual_nexthop_update(struct pbr_nexthop_cache
*pnhc
,
768 const struct pbr_nht_individual
*pnhi
)
770 assert(pnhi
->nhr
|| pnhi
->ifp
); /* Either nexthop or interface update */
772 switch (pnhc
->nexthop
->type
) {
773 case NEXTHOP_TYPE_IFINDEX
:
774 pbr_nht_individual_nexthop_interface_update(pnhc
, pnhi
);
776 case NEXTHOP_TYPE_IPV6_IFINDEX
:
777 if (IN6_IS_ADDR_LINKLOCAL(&pnhc
->nexthop
->gate
.ipv6
)) {
778 pbr_nht_individual_nexthop_interface_update(pnhc
, pnhi
);
781 /* Intentional fall thru */
782 case NEXTHOP_TYPE_IPV4_IFINDEX
:
783 case NEXTHOP_TYPE_IPV4
:
784 case NEXTHOP_TYPE_IPV6
:
785 pbr_nht_individual_nexthop_gw_update(pnhc
, pnhi
);
787 case NEXTHOP_TYPE_BLACKHOLE
:
793 static void pbr_nht_individual_nexthop_update_lookup(struct hash_bucket
*b
,
796 struct pbr_nexthop_cache
*pnhc
= b
->data
;
797 struct pbr_nht_individual
*pnhi
= data
;
798 char buf
[PREFIX_STRLEN
];
801 old_valid
= pnhc
->valid
;
803 pbr_nht_individual_nexthop_update(pnhc
, pnhi
);
805 DEBUGD(&pbr_dbg_nht
, "\tFound %s: old: %d new: %d",
806 prefix2str(&pnhi
->nhr
->prefix
, buf
, sizeof(buf
)), old_valid
,
813 static void pbr_nexthop_group_cache_iterate_to_group(struct hash_bucket
*b
,
816 struct pbr_nexthop_cache
*pnhc
= b
->data
;
817 struct nexthop_group
*nhg
= data
;
818 struct nexthop
*nh
= NULL
;
820 copy_nexthops(&nh
, pnhc
->nexthop
, NULL
);
822 _nexthop_add(&nhg
->nexthop
, nh
);
826 pbr_nexthop_group_cache_to_nexthop_group(struct nexthop_group
*nhg
,
827 struct pbr_nexthop_group_cache
*pnhgc
)
829 hash_iterate(pnhgc
->nhh
, pbr_nexthop_group_cache_iterate_to_group
, nhg
);
832 static void pbr_nht_nexthop_update_lookup(struct hash_bucket
*b
, void *data
)
834 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
835 struct pbr_nht_individual pnhi
= {};
836 struct nexthop_group nhg
= {};
839 old_valid
= pnhgc
->valid
;
841 pnhi
.nhr
= (struct zapi_route
*)data
;
843 hash_iterate(pnhgc
->nhh
, pbr_nht_individual_nexthop_update_lookup
,
847 * If any of the specified nexthops are valid we are valid
849 pnhgc
->valid
= !!pnhi
.valid
;
852 pbr_nexthop_group_cache_to_nexthop_group(&nhg
, pnhgc
);
853 pbr_nht_install_nexthop_group(pnhgc
, nhg
);
854 /* Don't need copied nexthops anymore */
855 nexthops_free(nhg
.nexthop
);
858 if (old_valid
!= pnhgc
->valid
)
859 pbr_map_check_nh_group_change(pnhgc
->name
);
862 void pbr_nht_nexthop_update(struct zapi_route
*nhr
)
864 hash_iterate(pbr_nhg_hash
, pbr_nht_nexthop_update_lookup
, nhr
);
868 pbr_nht_individual_nexthop_interface_update_lookup(struct hash_backet
*b
,
871 struct pbr_nexthop_cache
*pnhc
= b
->data
;
872 struct pbr_nht_individual
*pnhi
= data
;
875 old_valid
= pnhc
->valid
;
877 pbr_nht_individual_nexthop_update(pnhc
, pnhi
);
879 DEBUGD(&pbr_dbg_nht
, "\tFound %s: old: %d new: %d", pnhi
->ifp
->name
,
880 old_valid
, pnhc
->valid
);
886 static void pbr_nht_nexthop_interface_update_lookup(struct hash_backet
*b
,
889 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
890 struct pbr_nht_individual pnhi
= {};
893 old_valid
= pnhgc
->valid
;
897 hash_iterate(pnhgc
->nhh
,
898 pbr_nht_individual_nexthop_interface_update_lookup
, &pnhi
);
901 * If any of the specified nexthops are valid we are valid
903 pnhgc
->valid
= !!pnhi
.valid
;
905 if (old_valid
!= pnhgc
->valid
)
906 pbr_map_check_nh_group_change(pnhgc
->name
);
909 void pbr_nht_nexthop_interface_update(struct interface
*ifp
)
911 hash_iterate(pbr_nhg_hash
, pbr_nht_nexthop_interface_update_lookup
,
915 static uint32_t pbr_nhg_hash_key(const void *arg
)
917 const struct pbr_nexthop_group_cache
*nhgc
= arg
;
919 return jhash(&nhgc
->name
, strlen(nhgc
->name
), 0x52c34a96);
922 static bool pbr_nhg_hash_equal(const void *arg1
, const void *arg2
)
924 const struct pbr_nexthop_group_cache
*nhgc1
=
925 (const struct pbr_nexthop_group_cache
*)arg1
;
926 const struct pbr_nexthop_group_cache
*nhgc2
=
927 (const struct pbr_nexthop_group_cache
*)arg2
;
929 return !strcmp(nhgc1
->name
, nhgc2
->name
);
932 uint32_t pbr_nht_get_next_tableid(bool peek
)
937 for (i
= pbr_nhg_low_table
; i
<= pbr_nhg_high_table
; i
++) {
938 if (!nhg_tableid
[i
]) {
945 nhg_tableid
[i
] = !peek
;
951 void pbr_nht_set_tableid_range(uint32_t low
, uint32_t high
)
953 pbr_nhg_low_table
= low
;
954 pbr_nhg_high_table
= high
;
957 void pbr_nht_write_table_range(struct vty
*vty
)
959 if (pbr_nhg_low_table
!= PBR_NHT_DEFAULT_LOW_TABLEID
960 || pbr_nhg_high_table
!= PBR_NHT_DEFAULT_HIGH_TABLEID
) {
961 vty_out(vty
, "pbr table range %u %u\n", pbr_nhg_low_table
,
966 uint32_t pbr_nht_get_next_rule(uint32_t seqno
)
968 return seqno
+ pbr_nhg_low_rule
- 1;
970 void pbr_nht_set_rule_range(uint32_t low
, uint32_t high
)
972 pbr_nhg_low_rule
= low
;
973 pbr_nhg_high_rule
= high
;
976 void pbr_nht_write_rule_range(struct vty
*vty
)
978 if (pbr_nhg_low_rule
!= PBR_NHT_DEFAULT_LOW_RULE
979 || pbr_nhg_high_rule
!= PBR_NHT_DEFAULT_HIGH_RULE
) {
980 vty_out(vty
, "pbr rule range %u %u\n", pbr_nhg_low_rule
,
985 uint32_t pbr_nht_get_table(const char *name
)
987 struct pbr_nexthop_group_cache find
;
988 struct pbr_nexthop_group_cache
*pnhgc
;
990 memset(&find
, 0, sizeof(find
));
991 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
992 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
996 "%s: Could not find nexthop-group cache w/ name '%s'",
997 __PRETTY_FUNCTION__
, name
);
1001 return pnhgc
->table_id
;
1004 bool pbr_nht_get_installed(const char *name
)
1006 struct pbr_nexthop_group_cache find
;
1007 struct pbr_nexthop_group_cache
*pnhgc
;
1009 memset(&find
, 0, sizeof(find
));
1010 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
1012 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
1017 return pnhgc
->installed
;
1020 static void pbr_nht_show_nhg_nexthops(struct hash_bucket
*b
, void *data
)
1022 struct pbr_nexthop_cache
*pnhc
= b
->data
;
1023 struct vty
*vty
= data
;
1025 vty_out(vty
, "\tValid: %d ", pnhc
->valid
);
1026 nexthop_group_write_nexthop(vty
, pnhc
->nexthop
);
1029 struct pbr_nht_show
{
1034 static void pbr_nht_show_nhg(struct hash_bucket
*b
, void *data
)
1036 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
1037 struct pbr_nht_show
*pns
= data
;
1040 if (pns
->name
&& strcmp(pns
->name
, pnhgc
->name
) != 0)
1044 vty_out(vty
, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n",
1045 pnhgc
->name
, pnhgc
->table_id
, pnhgc
->valid
, pnhgc
->installed
);
1047 hash_iterate(pnhgc
->nhh
, pbr_nht_show_nhg_nexthops
, vty
);
1050 void pbr_nht_show_nexthop_group(struct vty
*vty
, const char *name
)
1052 struct pbr_nht_show pns
;
1057 hash_iterate(pbr_nhg_hash
, pbr_nht_show_nhg
, &pns
);
1060 void pbr_nht_init(void)
1062 pbr_nhg_hash
= hash_create_size(
1063 16, pbr_nhg_hash_key
, pbr_nhg_hash_equal
, "PBR NHG Cache Hash");
1065 hash_create_size(16, (unsigned int (*)(const void *))nexthop_hash
,
1066 pbr_nhrc_hash_equal
, "PBR NH Hash");
1068 pbr_nhg_low_table
= PBR_NHT_DEFAULT_LOW_TABLEID
;
1069 pbr_nhg_high_table
= PBR_NHT_DEFAULT_HIGH_TABLEID
;
1070 pbr_nhg_low_rule
= PBR_NHT_DEFAULT_LOW_RULE
;
1071 pbr_nhg_high_rule
= PBR_NHT_DEFAULT_HIGH_RULE
;
1072 memset(&nhg_tableid
, 0, 65535 * sizeof(uint8_t));