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>
31 #include "pbrd/pbr_nht.h"
32 #include "pbrd/pbr_map.h"
33 #include "pbrd/pbr_zebra.h"
34 #include "pbrd/pbr_memory.h"
35 #include "pbrd/pbr_debug.h"
37 DEFINE_MTYPE_STATIC(PBRD
, PBR_NHG
, "PBR Nexthop Groups")
39 static struct hash
*pbr_nhg_hash
;
40 static struct hash
*pbr_nhrc_hash
;
42 static uint32_t pbr_nhg_low_table
;
43 static uint32_t pbr_nhg_high_table
;
44 static uint32_t pbr_nhg_low_rule
;
45 static uint32_t pbr_nhg_high_rule
;
46 static bool nhg_tableid
[65535];
48 static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
49 struct nexthop_group nhg
);
51 pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
52 struct nexthop_group nhg
,
53 enum nexthop_types_t nh_type
);
59 struct nexthop nexthop
;
60 unsigned int refcount
;
63 /* Hash functions for pbr_nhrc_hash ---------------------------------------- */
65 static void *pbr_nhrc_hash_alloc(void *p
)
67 struct nhrc
*nhrc
= XCALLOC(MTYPE_PBR_NHG
, sizeof(struct nhrc
));
68 nhrc
->nexthop
= *(struct nexthop
*)p
;
72 static bool pbr_nhrc_hash_equal(const void *arg1
, const void *arg2
)
74 const struct nexthop
*nh1
, *nh2
;
79 return nexthop_same(nh1
, nh2
);
82 /* ------------------------------------------------------------------------- */
84 static void *pbr_nh_alloc(void *p
)
86 struct pbr_nexthop_cache
*new;
87 struct pbr_nexthop_cache
*pnhc
= (struct pbr_nexthop_cache
*)p
;
90 new = XCALLOC(MTYPE_PBR_NHG
, sizeof(*new));
91 nhrc
= hash_get(pbr_nhrc_hash
, pnhc
->nexthop
, pbr_nhrc_hash_alloc
);
92 new->nexthop
= &nhrc
->nexthop
;
94 /* Decremented again in pbr_nh_delete */
97 DEBUGD(&pbr_dbg_nht
, "%s: Sending nexthop to Zebra",
100 pbr_send_rnh(new->nexthop
, true);
106 static void pbr_nh_delete(struct pbr_nexthop_cache
**pnhc
)
110 nhrc
= hash_lookup(pbr_nhrc_hash
, (*pnhc
)->nexthop
);
114 if (!nhrc
|| nhrc
->refcount
== 0) {
115 DEBUGD(&pbr_dbg_nht
, "%s: Removing nexthop from Zebra",
116 __PRETTY_FUNCTION__
);
117 pbr_send_rnh((*pnhc
)->nexthop
, false);
119 if (nhrc
&& nhrc
->refcount
== 0) {
120 hash_release(pbr_nhrc_hash
, nhrc
);
121 XFREE(MTYPE_PBR_NHG
, nhrc
);
124 XFREE(MTYPE_PBR_NHG
, *pnhc
);
127 static void pbr_nh_delete_iterate(struct hash_bucket
*b
, void *p
)
129 pbr_nh_delete((struct pbr_nexthop_cache
**)&b
->data
);
132 static uint32_t pbr_nh_hash_key(const void *arg
)
135 const struct pbr_nexthop_cache
*pbrnc
= arg
;
137 key
= nexthop_hash(pbrnc
->nexthop
);
142 static bool pbr_nh_hash_equal(const void *arg1
, const void *arg2
)
144 const struct pbr_nexthop_cache
*pbrnc1
=
145 (const struct pbr_nexthop_cache
*)arg1
;
146 const struct pbr_nexthop_cache
*pbrnc2
=
147 (const struct pbr_nexthop_cache
*)arg2
;
149 if (pbrnc1
->nexthop
->vrf_id
!= pbrnc2
->nexthop
->vrf_id
)
152 if (pbrnc1
->nexthop
->ifindex
!= pbrnc2
->nexthop
->ifindex
)
155 if (pbrnc1
->nexthop
->type
!= pbrnc2
->nexthop
->type
)
158 switch (pbrnc1
->nexthop
->type
) {
159 case NEXTHOP_TYPE_IFINDEX
:
160 return pbrnc1
->nexthop
->ifindex
== pbrnc2
->nexthop
->ifindex
;
161 case NEXTHOP_TYPE_IPV4_IFINDEX
:
162 case NEXTHOP_TYPE_IPV4
:
163 return pbrnc1
->nexthop
->gate
.ipv4
.s_addr
164 == pbrnc2
->nexthop
->gate
.ipv4
.s_addr
;
165 case NEXTHOP_TYPE_IPV6_IFINDEX
:
166 case NEXTHOP_TYPE_IPV6
:
167 return !memcmp(&pbrnc1
->nexthop
->gate
.ipv6
,
168 &pbrnc2
->nexthop
->gate
.ipv6
, 16);
169 case NEXTHOP_TYPE_BLACKHOLE
:
170 return pbrnc1
->nexthop
->bh_type
== pbrnc2
->nexthop
->bh_type
;
174 * We should not get here
179 static void pbr_nhgc_delete(struct pbr_nexthop_group_cache
*p
)
181 hash_iterate(p
->nhh
, pbr_nh_delete_iterate
, NULL
);
183 XFREE(MTYPE_PBR_NHG
, p
);
186 static void *pbr_nhgc_alloc(void *p
)
188 struct pbr_nexthop_group_cache
*new;
189 struct pbr_nexthop_group_cache
*pnhgc
=
190 (struct pbr_nexthop_group_cache
*)p
;
192 new = XCALLOC(MTYPE_PBR_NHG
, sizeof(*new));
194 strcpy(new->name
, pnhgc
->name
);
195 new->table_id
= pbr_nht_get_next_tableid(false);
197 DEBUGD(&pbr_dbg_nht
, "%s: NHT: %s assigned Table ID: %u",
198 __PRETTY_FUNCTION__
, new->name
, new->table_id
);
200 new->nhh
= hash_create_size(8, pbr_nh_hash_key
, pbr_nh_hash_equal
,
201 "PBR NH Cache Hash");
206 void pbr_nhgroup_add_cb(const char *name
)
208 struct pbr_nexthop_group_cache
*pnhgc
;
209 struct nexthop_group_cmd
*nhgc
;
211 nhgc
= nhgc_find(name
);
214 DEBUGD(&pbr_dbg_nht
, "%s: Could not find nhgc with name: %s\n",
215 __PRETTY_FUNCTION__
, name
);
219 pnhgc
= pbr_nht_add_group(name
);
224 DEBUGD(&pbr_dbg_nht
, "%s: Added nexthop-group %s", __PRETTY_FUNCTION__
,
227 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
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 __PRETTY_FUNCTION__
, 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 __PRETTY_FUNCTION__
, 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 struct interface
*ifp
;
271 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
273 pbr_nht_nexthop_interface_update(ifp
);
277 void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
278 const struct nexthop
*nhop
)
281 struct pbr_nexthop_group_cache pnhgc_find
= {};
282 struct pbr_nexthop_group_cache
*pnhgc
;
283 struct pbr_nexthop_cache pnhc_find
= {};
284 struct pbr_nexthop_cache
*pnhc
;
285 enum nexthop_types_t nh_type
= nhop
->type
;
287 /* find pnhgc by name */
288 strlcpy(pnhgc_find
.name
, nhgc
->name
, sizeof(pnhgc_find
.name
));
289 pnhgc
= hash_lookup(pbr_nhg_hash
, &pnhgc_find
);
291 /* delete pnhc from pnhgc->nhh */
292 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
293 pnhc
= hash_release(pnhgc
->nhh
, &pnhc_find
);
296 pbr_nh_delete(&pnhc
);
298 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
299 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
300 DEBUGD(&pbr_dbg_nht
, "%s: Removed %s from nexthop-group %s",
301 __PRETTY_FUNCTION__
, debugstr
, nhgc
->name
);
304 if (pnhgc
->nhh
->count
)
305 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
307 pbr_nht_uninstall_nexthop_group(pnhgc
, nhgc
->nhg
, nh_type
);
309 pbr_map_check_nh_group_change(nhgc
->name
);
312 void pbr_nhgroup_delete_cb(const char *name
)
314 DEBUGD(&pbr_dbg_nht
, "%s: Removed nexthop-group %s",
315 __PRETTY_FUNCTION__
, name
);
317 /* delete group from all pbrms's */
318 pbr_nht_delete_group(name
);
320 pbr_map_check_nh_group_change(name
);
324 static struct pbr_nexthop_cache
*pbr_nht_lookup_nexthop(struct nexthop
*nexthop
)
330 static void pbr_nht_find_nhg_from_table_install(struct hash_bucket
*b
,
333 struct pbr_nexthop_group_cache
*pnhgc
=
334 (struct pbr_nexthop_group_cache
*)b
->data
;
335 uint32_t *table_id
= (uint32_t *)data
;
337 if (pnhgc
->table_id
== *table_id
) {
338 DEBUGD(&pbr_dbg_nht
, "%s: Table ID (%u) matches %s",
339 __PRETTY_FUNCTION__
, *table_id
, pnhgc
->name
);
342 * If the table has been re-handled by zebra
343 * and we are already installed no need to do
346 if (!pnhgc
->installed
) {
347 pnhgc
->installed
= true;
348 pbr_map_schedule_policy_from_nhg(pnhgc
->name
);
353 void pbr_nht_route_installed_for_table(uint32_t table_id
)
355 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_install
,
359 static void pbr_nht_find_nhg_from_table_remove(struct hash_bucket
*b
,
365 void pbr_nht_route_removed_for_table(uint32_t table_id
)
367 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_remove
,
372 * Loop through all nexthops in a nexthop group to check that they are all the
373 * same. If they are not all the same, log this peculiarity.
376 * The nexthop group to check
379 * - AFI of last nexthop in the group
382 static afi_t
pbr_nht_which_afi(struct nexthop_group nhg
,
383 enum nexthop_types_t nh_type
)
385 struct nexthop
*nexthop
;
386 afi_t install_afi
= AFI_MAX
;
391 case NEXTHOP_TYPE_IPV4
:
392 case NEXTHOP_TYPE_IPV4_IFINDEX
:
394 case NEXTHOP_TYPE_IPV6
:
395 case NEXTHOP_TYPE_IPV6_IFINDEX
:
397 case NEXTHOP_TYPE_IFINDEX
:
398 case NEXTHOP_TYPE_BLACKHOLE
:
403 v6
= v4
= bh
= false;
405 for (ALL_NEXTHOPS(nhg
, nexthop
)) {
406 nh_type
= nexthop
->type
;
409 case NEXTHOP_TYPE_IFINDEX
:
411 case NEXTHOP_TYPE_IPV4
:
412 case NEXTHOP_TYPE_IPV4_IFINDEX
:
414 install_afi
= AFI_IP
;
416 case NEXTHOP_TYPE_IPV6
:
417 case NEXTHOP_TYPE_IPV6_IFINDEX
:
419 install_afi
= AFI_IP6
;
421 case NEXTHOP_TYPE_BLACKHOLE
:
427 /* Interface and/or blackhole nexthops only. */
429 install_afi
= AFI_MAX
;
433 "%s: Saw both V6 and V4 nexthops...using %s",
434 __PRETTY_FUNCTION__
, afi2str(install_afi
));
435 if (bh
&& (v6
|| v4
))
437 "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.",
438 __PRETTY_FUNCTION__
, v4
? "v4" : "",
439 (v4
&& v6
) ? " and " : "", v6
? "v6" : "");
444 static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
445 struct nexthop_group nhg
)
448 enum nexthop_types_t nh_type
= 0;
450 install_afi
= pbr_nht_which_afi(nhg
, nh_type
);
452 route_add(pnhgc
, nhg
, install_afi
);
456 pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
457 struct nexthop_group nhg
,
458 enum nexthop_types_t nh_type
)
462 install_afi
= pbr_nht_which_afi(nhg
, nh_type
);
464 pnhgc
->installed
= false;
465 pnhgc
->valid
= false;
466 route_delete(pnhgc
, install_afi
);
469 void pbr_nht_change_group(const char *name
)
471 struct nexthop_group_cmd
*nhgc
;
472 struct pbr_nexthop_group_cache
*pnhgc
;
473 struct pbr_nexthop_group_cache find
;
474 struct nexthop
*nhop
;
476 nhgc
= nhgc_find(name
);
480 memset(&find
, 0, sizeof(find
));
481 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
482 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
486 "%s: Could not find nexthop-group cache w/ name '%s'",
487 __PRETTY_FUNCTION__
, name
);
491 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
492 struct pbr_nexthop_cache lookup
;
493 struct pbr_nexthop_cache
*pnhc
;
495 lookup
.nexthop
= nhop
;
496 pnhc
= hash_lookup(pnhgc
->nhh
, &lookup
);
498 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
499 pnhc
->parent
= pnhgc
;
502 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
505 char *pbr_nht_nexthop_make_name(char *name
, size_t l
,
506 uint32_t seqno
, char *buffer
)
508 snprintf(buffer
, l
, "%s%u", name
, seqno
);
512 void pbr_nht_add_individual_nexthop(struct pbr_map_sequence
*pbrms
)
514 struct pbr_nexthop_group_cache
*pnhgc
;
515 struct pbr_nexthop_group_cache find
;
516 struct pbr_nexthop_cache
*pnhc
;
517 struct pbr_nexthop_cache lookup
;
519 memset(&find
, 0, sizeof(find
));
520 pbr_nht_nexthop_make_name(pbrms
->parent
->name
, PBR_NHC_NAMELEN
,
521 pbrms
->seqno
, find
.name
);
523 if (!pbr_nht_get_next_tableid(true)) {
525 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
526 __PRETTY_FUNCTION__
, find
.name
);
530 if (!pbrms
->internal_nhg_name
)
531 pbrms
->internal_nhg_name
= XSTRDUP(MTYPE_TMP
, find
.name
);
533 pnhgc
= hash_get(pbr_nhg_hash
, &find
, pbr_nhgc_alloc
);
535 lookup
.nexthop
= pbrms
->nhg
->nexthop
;
536 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
537 pnhc
->parent
= pnhgc
;
538 pbr_nht_install_nexthop_group(pnhgc
, *pbrms
->nhg
);
541 void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence
*pbrms
)
543 struct pbr_nexthop_group_cache
*pnhgc
;
544 struct pbr_nexthop_group_cache find
;
545 struct pbr_nexthop_cache
*pnhc
;
546 struct pbr_nexthop_cache lup
;
547 struct pbr_map
*pbrm
= pbrms
->parent
;
548 struct listnode
*node
;
549 struct pbr_map_interface
*pmi
;
551 enum nexthop_types_t nh_type
= 0;
553 if (pbrm
->valid
&& pbrms
->nhs_installed
&& pbrm
->incoming
->count
) {
554 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, node
, pmi
))
555 pbr_send_pbr_map(pbrms
, pmi
, false);
559 pbrms
->nhs_installed
= false;
560 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
562 memset(&find
, 0, sizeof(find
));
563 snprintf(find
.name
, sizeof(find
.name
), "%s", pbrms
->internal_nhg_name
);
564 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
566 nh
= pbrms
->nhg
->nexthop
;
569 pnhc
= hash_lookup(pnhgc
->nhh
, &lup
);
571 hash_release(pnhgc
->nhh
, pnhc
);
572 pbr_nh_delete(&pnhc
);
573 pbr_nht_uninstall_nexthop_group(pnhgc
, *pbrms
->nhg
, nh_type
);
575 hash_release(pbr_nhg_hash
, pnhgc
);
577 nexthop_del(pbrms
->nhg
, nh
);
579 nexthop_group_delete(&pbrms
->nhg
);
580 XFREE(MTYPE_TMP
, pbrms
->internal_nhg_name
);
583 struct pbr_nexthop_group_cache
*pbr_nht_add_group(const char *name
)
585 struct nexthop
*nhop
;
586 struct nexthop_group_cmd
*nhgc
;
587 struct pbr_nexthop_group_cache
*pnhgc
;
588 struct pbr_nexthop_group_cache lookup
;
590 if (!pbr_nht_get_next_tableid(true)) {
592 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
593 __PRETTY_FUNCTION__
, name
);
597 nhgc
= nhgc_find(name
);
600 DEBUGD(&pbr_dbg_nht
, "%s: Could not find nhgc with name: %s\n",
601 __PRETTY_FUNCTION__
, name
);
605 snprintf(lookup
.name
, sizeof(lookup
.name
), "%s", name
);
606 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, pbr_nhgc_alloc
);
607 DEBUGD(&pbr_dbg_nht
, "%s: Retrieved NHGC @ %p", __PRETTY_FUNCTION__
,
610 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
611 struct pbr_nexthop_cache lookupc
;
612 struct pbr_nexthop_cache
*pnhc
;
614 lookupc
.nexthop
= nhop
;
615 pnhc
= hash_lookup(pnhgc
->nhh
, &lookupc
);
617 pnhc
= hash_get(pnhgc
->nhh
, &lookupc
, pbr_nh_alloc
);
618 pnhc
->parent
= pnhgc
;
625 void pbr_nht_delete_group(const char *name
)
627 struct pbr_map_sequence
*pbrms
;
628 struct listnode
*snode
;
629 struct pbr_map
*pbrm
;
630 struct pbr_nexthop_group_cache pnhgc_find
;
631 struct pbr_nexthop_group_cache
*pnhgc
;
633 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
634 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, snode
, pbrms
)) {
635 if (pbrms
->nhgrp_name
636 && strmatch(pbrms
->nhgrp_name
, name
)) {
637 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
638 nexthop_group_delete(&pbrms
->nhg
);
640 pbrms
->internal_nhg_name
= NULL
;
646 strlcpy(pnhgc_find
.name
, name
, sizeof(pnhgc_find
.name
));
647 pnhgc
= hash_release(pbr_nhg_hash
, &pnhgc_find
);
648 pbr_nhgc_delete(pnhgc
);
651 bool pbr_nht_nexthop_valid(struct nexthop_group
*nhg
)
653 DEBUGD(&pbr_dbg_nht
, "%s: %p", __PRETTY_FUNCTION__
, nhg
);
657 bool pbr_nht_nexthop_group_valid(const char *name
)
659 struct pbr_nexthop_group_cache
*pnhgc
;
660 struct pbr_nexthop_group_cache lookup
;
662 DEBUGD(&pbr_dbg_nht
, "%s: %s", __PRETTY_FUNCTION__
, name
);
664 snprintf(lookup
.name
, sizeof(lookup
.name
), "%s", name
);
665 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, NULL
);
668 DEBUGD(&pbr_dbg_nht
, "%s: \t%d %d", __PRETTY_FUNCTION__
, pnhgc
->valid
,
670 if (pnhgc
->valid
&& pnhgc
->installed
)
676 struct pbr_nht_individual
{
677 struct zapi_route
*nhr
;
678 struct interface
*ifp
;
683 static void pbr_nht_individual_nexthop_update_lookup(struct hash_bucket
*b
,
686 struct pbr_nexthop_cache
*pnhc
= b
->data
;
687 struct pbr_nht_individual
*pnhi
= data
;
688 char buf
[PREFIX_STRLEN
];
691 old_valid
= pnhc
->valid
;
693 switch (pnhi
->nhr
->prefix
.family
) {
695 if (pnhc
->nexthop
->gate
.ipv4
.s_addr
696 == pnhi
->nhr
->prefix
.u
.prefix4
.s_addr
)
697 pnhc
->valid
= !!pnhi
->nhr
->nexthop_num
;
700 if (memcmp(&pnhc
->nexthop
->gate
.ipv6
,
701 &pnhi
->nhr
->prefix
.u
.prefix6
, 16)
703 pnhc
->valid
= !!pnhi
->nhr
->nexthop_num
;
707 DEBUGD(&pbr_dbg_nht
, "\tFound %s: old: %d new: %d",
708 prefix2str(&pnhi
->nhr
->prefix
, buf
, sizeof(buf
)), old_valid
,
715 static void pbr_nht_nexthop_update_lookup(struct hash_bucket
*b
, void *data
)
717 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
718 struct pbr_nht_individual pnhi
;
721 old_valid
= pnhgc
->valid
;
723 pnhi
.nhr
= (struct zapi_route
*)data
;
725 hash_iterate(pnhgc
->nhh
, pbr_nht_individual_nexthop_update_lookup
,
729 * If any of the specified nexthops are valid we are valid
731 pnhgc
->valid
= !!pnhi
.valid
;
733 if (old_valid
!= pnhgc
->valid
)
734 pbr_map_check_nh_group_change(pnhgc
->name
);
737 void pbr_nht_nexthop_update(struct zapi_route
*nhr
)
739 hash_iterate(pbr_nhg_hash
, pbr_nht_nexthop_update_lookup
, nhr
);
743 pbr_nht_individual_nexthop_interface_update_lookup(struct hash_backet
*b
,
746 struct pbr_nexthop_cache
*pnhc
= b
->data
;
747 struct pbr_nht_individual
*pnhi
= data
;
750 old_valid
= pnhc
->valid
;
752 if (pnhc
->nexthop
->type
== NEXTHOP_TYPE_IFINDEX
753 && pnhc
->nexthop
->ifindex
== pnhi
->ifp
->ifindex
)
754 pnhc
->valid
= !!if_is_up(pnhi
->ifp
);
756 DEBUGD(&pbr_dbg_nht
, "\tFound %s: old: %d new: %d", pnhi
->ifp
->name
,
757 old_valid
, pnhc
->valid
);
763 static void pbr_nht_nexthop_interface_update_lookup(struct hash_backet
*b
,
766 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
767 struct pbr_nht_individual pnhi
;
770 old_valid
= pnhgc
->valid
;
774 hash_iterate(pnhgc
->nhh
,
775 pbr_nht_individual_nexthop_interface_update_lookup
, &pnhi
);
778 * If any of the specified nexthops are valid we are valid
780 pnhgc
->valid
= !!pnhi
.valid
;
782 if (old_valid
!= pnhgc
->valid
)
783 pbr_map_check_nh_group_change(pnhgc
->name
);
786 void pbr_nht_nexthop_interface_update(struct interface
*ifp
)
788 hash_iterate(pbr_nhg_hash
, pbr_nht_nexthop_interface_update_lookup
,
792 static uint32_t pbr_nhg_hash_key(const void *arg
)
794 const struct pbr_nexthop_group_cache
*nhgc
= arg
;
796 return jhash(&nhgc
->name
, strlen(nhgc
->name
), 0x52c34a96);
799 static bool pbr_nhg_hash_equal(const void *arg1
, const void *arg2
)
801 const struct pbr_nexthop_group_cache
*nhgc1
=
802 (const struct pbr_nexthop_group_cache
*)arg1
;
803 const struct pbr_nexthop_group_cache
*nhgc2
=
804 (const struct pbr_nexthop_group_cache
*)arg2
;
806 return !strcmp(nhgc1
->name
, nhgc2
->name
);
809 uint32_t pbr_nht_get_next_tableid(bool peek
)
814 for (i
= pbr_nhg_low_table
; i
<= pbr_nhg_high_table
; i
++) {
815 if (!nhg_tableid
[i
]) {
822 nhg_tableid
[i
] = !peek
;
828 void pbr_nht_set_tableid_range(uint32_t low
, uint32_t high
)
830 pbr_nhg_low_table
= low
;
831 pbr_nhg_high_table
= high
;
834 void pbr_nht_write_table_range(struct vty
*vty
)
836 if (pbr_nhg_low_table
!= PBR_NHT_DEFAULT_LOW_TABLEID
837 || pbr_nhg_high_table
!= PBR_NHT_DEFAULT_HIGH_TABLEID
) {
838 vty_out(vty
, "pbr table range %u %u\n", pbr_nhg_low_table
,
843 uint32_t pbr_nht_get_next_rule(uint32_t seqno
)
845 return seqno
+ pbr_nhg_low_rule
- 1;
847 void pbr_nht_set_rule_range(uint32_t low
, uint32_t high
)
849 pbr_nhg_low_rule
= low
;
850 pbr_nhg_high_rule
= high
;
853 void pbr_nht_write_rule_range(struct vty
*vty
)
855 if (pbr_nhg_low_rule
!= PBR_NHT_DEFAULT_LOW_RULE
856 || pbr_nhg_high_rule
!= PBR_NHT_DEFAULT_HIGH_RULE
) {
857 vty_out(vty
, "pbr rule range %u %u\n", pbr_nhg_low_rule
,
862 uint32_t pbr_nht_get_table(const char *name
)
864 struct pbr_nexthop_group_cache find
;
865 struct pbr_nexthop_group_cache
*pnhgc
;
867 memset(&find
, 0, sizeof(find
));
868 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
869 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
873 "%s: Could not find nexthop-group cache w/ name '%s'",
874 __PRETTY_FUNCTION__
, name
);
878 return pnhgc
->table_id
;
881 bool pbr_nht_get_installed(const char *name
)
883 struct pbr_nexthop_group_cache find
;
884 struct pbr_nexthop_group_cache
*pnhgc
;
886 memset(&find
, 0, sizeof(find
));
887 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
889 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
894 return pnhgc
->installed
;
897 static void pbr_nht_show_nhg_nexthops(struct hash_bucket
*b
, void *data
)
899 struct pbr_nexthop_cache
*pnhc
= b
->data
;
900 struct vty
*vty
= data
;
902 vty_out(vty
, "\tValid: %d ", pnhc
->valid
);
903 nexthop_group_write_nexthop(vty
, pnhc
->nexthop
);
906 struct pbr_nht_show
{
911 static void pbr_nht_show_nhg(struct hash_bucket
*b
, void *data
)
913 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
914 struct pbr_nht_show
*pns
= data
;
917 if (pns
->name
&& strcmp(pns
->name
, pnhgc
->name
) != 0)
921 vty_out(vty
, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n",
922 pnhgc
->name
, pnhgc
->table_id
, pnhgc
->valid
, pnhgc
->installed
);
924 hash_iterate(pnhgc
->nhh
, pbr_nht_show_nhg_nexthops
, vty
);
927 void pbr_nht_show_nexthop_group(struct vty
*vty
, const char *name
)
929 struct pbr_nht_show pns
;
934 hash_iterate(pbr_nhg_hash
, pbr_nht_show_nhg
, &pns
);
937 void pbr_nht_init(void)
939 pbr_nhg_hash
= hash_create_size(
940 16, pbr_nhg_hash_key
, pbr_nhg_hash_equal
, "PBR NHG Cache Hash");
942 hash_create_size(16, (unsigned int (*)(const void *))nexthop_hash
,
943 pbr_nhrc_hash_equal
, "PBR NH Hash");
945 pbr_nhg_low_table
= PBR_NHT_DEFAULT_LOW_TABLEID
;
946 pbr_nhg_high_table
= PBR_NHT_DEFAULT_HIGH_TABLEID
;
947 pbr_nhg_low_rule
= PBR_NHT_DEFAULT_LOW_RULE
;
948 pbr_nhg_high_rule
= PBR_NHT_DEFAULT_HIGH_RULE
;
949 memset(&nhg_tableid
, 0, 65535 * sizeof(uint8_t));