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_afi
);
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 int 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_backet
*b
, void *p
)
129 pbr_nh_delete((struct pbr_nexthop_cache
**)&b
->data
);
132 static uint32_t pbr_nh_hash_key(void *arg
)
135 struct pbr_nexthop_cache
*pbrnc
= (struct pbr_nexthop_cache
*)arg
;
137 key
= nexthop_hash(pbrnc
->nexthop
);
142 static int 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
:
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();
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
);
221 DEBUGD(&pbr_dbg_nht
, "%s: Added nexthop-group %s", __PRETTY_FUNCTION__
,
224 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
225 pbr_map_check_nh_group_change(name
);
228 void pbr_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
229 const struct nexthop
*nhop
)
232 struct pbr_nexthop_group_cache pnhgc_find
= {};
233 struct pbr_nexthop_group_cache
*pnhgc
;
234 struct pbr_nexthop_cache pnhc_find
= {};
235 struct pbr_nexthop_cache
*pnhc
;
237 /* find pnhgc by name */
238 strlcpy(pnhgc_find
.name
, nhgc
->name
, sizeof(pnhgc_find
.name
));
239 pnhgc
= hash_get(pbr_nhg_hash
, &pnhgc_find
, pbr_nhgc_alloc
);
241 /* create & insert new pnhc into pnhgc->nhh */
242 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
243 pnhc
= hash_get(pnhgc
->nhh
, &pnhc_find
, pbr_nh_alloc
);
244 pnhc_find
.nexthop
= NULL
;
246 /* set parent pnhgc */
247 pnhc
->parent
= pnhgc
;
249 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
250 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
251 DEBUGD(&pbr_dbg_nht
, "%s: Added %s to nexthop-group %s",
252 __PRETTY_FUNCTION__
, debugstr
, nhgc
->name
);
255 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
256 pbr_map_check_nh_group_change(nhgc
->name
);
259 void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
260 const struct nexthop
*nhop
)
263 struct pbr_nexthop_group_cache pnhgc_find
= {};
264 struct pbr_nexthop_group_cache
*pnhgc
;
265 struct pbr_nexthop_cache pnhc_find
= {};
266 struct pbr_nexthop_cache
*pnhc
;
267 enum nexthop_types_t nh_afi
= nhop
->type
;
269 /* find pnhgc by name */
270 strlcpy(pnhgc_find
.name
, nhgc
->name
, sizeof(pnhgc_find
.name
));
271 pnhgc
= hash_get(pbr_nhg_hash
, &pnhgc_find
, pbr_nhgc_alloc
);
273 /* delete pnhc from pnhgc->nhh */
274 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
275 pnhc
= hash_release(pnhgc
->nhh
, &pnhc_find
);
278 pbr_nh_delete(&pnhc
);
280 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
281 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
282 DEBUGD(&pbr_dbg_nht
, "%s: Removed %s from nexthop-group %s",
283 __PRETTY_FUNCTION__
, debugstr
, nhgc
->name
);
286 if (pnhgc
->nhh
->count
)
287 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
289 pbr_nht_uninstall_nexthop_group(pnhgc
, nhgc
->nhg
, nh_afi
);
291 pbr_map_check_nh_group_change(nhgc
->name
);
294 void pbr_nhgroup_delete_cb(const char *name
)
296 DEBUGD(&pbr_dbg_nht
, "%s: Removed nexthop-group %s",
297 __PRETTY_FUNCTION__
, name
);
299 /* delete group from all pbrms's */
300 pbr_nht_delete_group(name
);
302 pbr_map_check_nh_group_change(name
);
306 static struct pbr_nexthop_cache
*pbr_nht_lookup_nexthop(struct nexthop
*nexthop
)
312 static void pbr_nht_find_nhg_from_table_install(struct hash_backet
*b
,
315 struct pbr_nexthop_group_cache
*pnhgc
=
316 (struct pbr_nexthop_group_cache
*)b
->data
;
317 uint32_t *table_id
= (uint32_t *)data
;
319 if (pnhgc
->table_id
== *table_id
) {
320 DEBUGD(&pbr_dbg_nht
, "%s: Table ID (%u) matches %s",
321 __PRETTY_FUNCTION__
, *table_id
, pnhgc
->name
);
324 * If the table has been re-handled by zebra
325 * and we are already installed no need to do
328 if (!pnhgc
->installed
) {
329 pnhgc
->installed
= true;
330 pbr_map_schedule_policy_from_nhg(pnhgc
->name
);
335 void pbr_nht_route_installed_for_table(uint32_t table_id
)
337 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_install
,
341 static void pbr_nht_find_nhg_from_table_remove(struct hash_backet
*b
,
347 void pbr_nht_route_removed_for_table(uint32_t table_id
)
349 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_remove
,
354 * Loop through all nexthops in a nexthop group to check that they are all the
355 * same. If they are not all the same, log this peculiarity.
358 * The nexthop group to check
361 * - AFI of last nexthop in the group
364 static afi_t
pbr_nht_which_afi(struct nexthop_group nhg
,
365 enum nexthop_types_t nh_afi
)
367 struct nexthop
*nexthop
;
368 afi_t install_afi
= AFI_MAX
;
371 v6
= v4
= bh
= false;
374 for (ALL_NEXTHOPS(nhg
, nexthop
)) {
375 nh_afi
= nexthop
->type
;
381 case NEXTHOP_TYPE_IFINDEX
:
383 case NEXTHOP_TYPE_IPV4
:
384 case NEXTHOP_TYPE_IPV4_IFINDEX
:
386 install_afi
= AFI_IP
;
388 case NEXTHOP_TYPE_IPV6
:
389 case NEXTHOP_TYPE_IPV6_IFINDEX
:
391 install_afi
= AFI_IP6
;
393 case NEXTHOP_TYPE_BLACKHOLE
:
395 install_afi
= AFI_MAX
;
401 "%s: Saw both V6 and V4 nexthops...using %s",
402 __PRETTY_FUNCTION__
, afi2str(install_afi
));
403 if (bh
&& (v6
|| v4
))
405 "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.",
406 __PRETTY_FUNCTION__
, v4
? "v4" : "",
407 (v4
&& v6
) ? " and " : "", v6
? "v6" : "");
412 static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
413 struct nexthop_group nhg
)
416 enum nexthop_types_t nh_afi
= 0;
418 install_afi
= pbr_nht_which_afi(nhg
, nh_afi
);
420 route_add(pnhgc
, nhg
, install_afi
);
424 pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
425 struct nexthop_group nhg
,
426 enum nexthop_types_t nh_afi
)
430 install_afi
= pbr_nht_which_afi(nhg
, nh_afi
);
432 pnhgc
->installed
= false;
433 pnhgc
->valid
= false;
434 route_delete(pnhgc
, install_afi
);
437 void pbr_nht_change_group(const char *name
)
439 struct nexthop_group_cmd
*nhgc
;
440 struct pbr_nexthop_group_cache
*pnhgc
;
441 struct pbr_nexthop_group_cache find
;
442 struct nexthop
*nhop
;
444 nhgc
= nhgc_find(name
);
448 memset(&find
, 0, sizeof(find
));
449 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
450 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
454 "%s: Could not find nexthop-group cache w/ name '%s'",
455 __PRETTY_FUNCTION__
, name
);
459 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
460 struct pbr_nexthop_cache lookup
;
461 struct pbr_nexthop_cache
*pnhc
;
463 lookup
.nexthop
= nhop
;
464 pnhc
= hash_lookup(pnhgc
->nhh
, &lookup
);
466 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
467 pnhc
->parent
= pnhgc
;
470 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
473 char *pbr_nht_nexthop_make_name(char *name
, size_t l
,
474 uint32_t seqno
, char *buffer
)
476 snprintf(buffer
, l
, "%s%u", name
, seqno
);
480 void pbr_nht_add_individual_nexthop(struct pbr_map_sequence
*pbrms
)
482 struct pbr_nexthop_group_cache
*pnhgc
;
483 struct pbr_nexthop_group_cache find
;
484 struct pbr_nexthop_cache
*pnhc
;
485 struct pbr_nexthop_cache lookup
;
487 memset(&find
, 0, sizeof(find
));
488 pbr_nht_nexthop_make_name(pbrms
->parent
->name
, PBR_NHC_NAMELEN
,
489 pbrms
->seqno
, find
.name
);
490 if (!pbrms
->internal_nhg_name
)
491 pbrms
->internal_nhg_name
= XSTRDUP(MTYPE_TMP
, find
.name
);
493 pnhgc
= hash_get(pbr_nhg_hash
, &find
, pbr_nhgc_alloc
);
495 lookup
.nexthop
= pbrms
->nhg
->nexthop
;
496 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
497 pnhc
->parent
= pnhgc
;
498 pbr_nht_install_nexthop_group(pnhgc
, *pbrms
->nhg
);
501 void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence
*pbrms
)
503 struct pbr_nexthop_group_cache
*pnhgc
;
504 struct pbr_nexthop_group_cache find
;
505 struct pbr_nexthop_cache
*pnhc
;
506 struct pbr_nexthop_cache lup
;
507 struct pbr_map
*pbrm
= pbrms
->parent
;
508 struct listnode
*node
;
509 struct pbr_map_interface
*pmi
;
511 enum nexthop_types_t nh_afi
= 0;
513 if (pbrm
->valid
&& pbrms
->nhs_installed
&& pbrm
->incoming
->count
) {
514 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, node
, pmi
))
515 pbr_send_pbr_map(pbrms
, pmi
, false);
519 pbrms
->nhs_installed
= false;
520 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
522 memset(&find
, 0, sizeof(find
));
523 snprintf(find
.name
, sizeof(find
.name
), "%s", pbrms
->internal_nhg_name
);
524 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
526 nh
= pbrms
->nhg
->nexthop
;
529 pnhc
= hash_lookup(pnhgc
->nhh
, &lup
);
531 hash_release(pnhgc
->nhh
, pnhc
);
532 pbr_nh_delete(&pnhc
);
533 pbr_nht_uninstall_nexthop_group(pnhgc
, *pbrms
->nhg
, nh_afi
);
535 hash_release(pbr_nhg_hash
, pnhgc
);
537 nexthop_del(pbrms
->nhg
, nh
);
539 nexthop_group_delete(&pbrms
->nhg
);
540 XFREE(MTYPE_TMP
, pbrms
->internal_nhg_name
);
543 struct pbr_nexthop_group_cache
*pbr_nht_add_group(const char *name
)
545 struct nexthop
*nhop
;
546 struct nexthop_group_cmd
*nhgc
;
547 struct pbr_nexthop_group_cache
*pnhgc
;
548 struct pbr_nexthop_group_cache lookup
;
550 nhgc
= nhgc_find(name
);
553 zlog_warn("%s: Could not find group %s to add",
554 __PRETTY_FUNCTION__
, name
);
558 snprintf(lookup
.name
, sizeof(lookup
.name
), "%s", name
);
559 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, pbr_nhgc_alloc
);
560 DEBUGD(&pbr_dbg_nht
, "%s: Retrieved NHGC @ %p", __PRETTY_FUNCTION__
,
563 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
564 struct pbr_nexthop_cache lookup
;
565 struct pbr_nexthop_cache
*pnhc
;
567 lookup
.nexthop
= nhop
;
568 pnhc
= hash_lookup(pnhgc
->nhh
, &lookup
);
570 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
571 pnhc
->parent
= pnhgc
;
578 void pbr_nht_delete_group(const char *name
)
580 struct pbr_map_sequence
*pbrms
;
581 struct listnode
*snode
;
582 struct pbr_map
*pbrm
;
583 struct pbr_nexthop_group_cache pnhgc_find
;
584 struct pbr_nexthop_group_cache
*pnhgc
;
586 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
587 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, snode
, pbrms
)) {
588 if (pbrms
->nhgrp_name
589 && strmatch(pbrms
->nhgrp_name
, name
)) {
590 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
591 nexthop_group_delete(&pbrms
->nhg
);
593 pbrms
->internal_nhg_name
= NULL
;
599 strlcpy(pnhgc_find
.name
, name
, sizeof(pnhgc_find
.name
));
600 pnhgc
= hash_release(pbr_nhg_hash
, &pnhgc_find
);
601 pbr_nhgc_delete(pnhgc
);
604 bool pbr_nht_nexthop_valid(struct nexthop_group
*nhg
)
606 DEBUGD(&pbr_dbg_nht
, "%s: %p", __PRETTY_FUNCTION__
, nhg
);
610 bool pbr_nht_nexthop_group_valid(const char *name
)
612 struct pbr_nexthop_group_cache
*pnhgc
;
613 struct pbr_nexthop_group_cache lookup
;
615 DEBUGD(&pbr_dbg_nht
, "%s: %s", __PRETTY_FUNCTION__
, name
);
617 snprintf(lookup
.name
, sizeof(lookup
.name
), "%s", name
);
618 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, NULL
);
621 DEBUGD(&pbr_dbg_nht
, "%s: \t%d %d", __PRETTY_FUNCTION__
, pnhgc
->valid
,
623 if (pnhgc
->valid
&& pnhgc
->installed
)
629 struct pbr_nht_individual
{
630 struct zapi_route
*nhr
;
635 static void pbr_nht_individual_nexthop_update_lookup(struct hash_backet
*b
,
638 struct pbr_nexthop_cache
*pnhc
= b
->data
;
639 struct pbr_nht_individual
*pnhi
= data
;
640 char buf
[PREFIX_STRLEN
];
643 old_valid
= pnhc
->valid
;
645 switch (pnhi
->nhr
->prefix
.family
) {
647 if (pnhc
->nexthop
->gate
.ipv4
.s_addr
648 == pnhi
->nhr
->prefix
.u
.prefix4
.s_addr
)
649 pnhc
->valid
= !!pnhi
->nhr
->nexthop_num
;
652 if (memcmp(&pnhc
->nexthop
->gate
.ipv6
,
653 &pnhi
->nhr
->prefix
.u
.prefix6
, 16)
655 pnhc
->valid
= !!pnhi
->nhr
->nexthop_num
;
659 DEBUGD(&pbr_dbg_nht
, "\tFound %s: old: %d new: %d",
660 prefix2str(&pnhi
->nhr
->prefix
, buf
, sizeof(buf
)), old_valid
,
667 static void pbr_nht_nexthop_update_lookup(struct hash_backet
*b
, void *data
)
669 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
670 struct pbr_nht_individual pnhi
;
673 old_valid
= pnhgc
->valid
;
675 pnhi
.nhr
= (struct zapi_route
*)data
;
677 hash_iterate(pnhgc
->nhh
, pbr_nht_individual_nexthop_update_lookup
,
681 * If any of the specified nexthops are valid we are valid
683 pnhgc
->valid
= !!pnhi
.valid
;
685 if (old_valid
!= pnhgc
->valid
)
686 pbr_map_check_nh_group_change(pnhgc
->name
);
689 void pbr_nht_nexthop_update(struct zapi_route
*nhr
)
691 hash_iterate(pbr_nhg_hash
, pbr_nht_nexthop_update_lookup
, nhr
);
694 static uint32_t pbr_nhg_hash_key(void *arg
)
696 struct pbr_nexthop_group_cache
*nhgc
=
697 (struct pbr_nexthop_group_cache
*)arg
;
699 return jhash(&nhgc
->name
, strlen(nhgc
->name
), 0x52c34a96);
702 static int pbr_nhg_hash_equal(const void *arg1
, const void *arg2
)
704 const struct pbr_nexthop_group_cache
*nhgc1
=
705 (const struct pbr_nexthop_group_cache
*)arg1
;
706 const struct pbr_nexthop_group_cache
*nhgc2
=
707 (const struct pbr_nexthop_group_cache
*)arg2
;
709 return !strcmp(nhgc1
->name
, nhgc2
->name
);
713 uint32_t pbr_nht_get_next_tableid(void)
718 for (i
= pbr_nhg_low_table
; i
<= pbr_nhg_high_table
; i
++) {
719 if (nhg_tableid
[i
] == false) {
726 nhg_tableid
[i
] = true;
732 void pbr_nht_set_tableid_range(uint32_t low
, uint32_t high
)
734 pbr_nhg_low_table
= low
;
735 pbr_nhg_high_table
= high
;
738 void pbr_nht_write_table_range(struct vty
*vty
)
740 if (pbr_nhg_low_table
!= PBR_NHT_DEFAULT_LOW_TABLEID
741 || pbr_nhg_high_table
!= PBR_NHT_DEFAULT_HIGH_TABLEID
) {
742 vty_out(vty
, "pbr table range %u %u\n", pbr_nhg_low_table
,
747 uint32_t pbr_nht_get_next_rule(uint32_t seqno
)
749 return seqno
+ pbr_nhg_low_rule
- 1;
751 void pbr_nht_set_rule_range(uint32_t low
, uint32_t high
)
753 pbr_nhg_low_rule
= low
;
754 pbr_nhg_high_rule
= high
;
757 void pbr_nht_write_rule_range(struct vty
*vty
)
759 if (pbr_nhg_low_rule
!= PBR_NHT_DEFAULT_LOW_RULE
760 || pbr_nhg_high_rule
!= PBR_NHT_DEFAULT_HIGH_RULE
) {
761 vty_out(vty
, "pbr rule range %u %u\n", pbr_nhg_low_rule
,
766 uint32_t pbr_nht_get_table(const char *name
)
768 struct pbr_nexthop_group_cache find
;
769 struct pbr_nexthop_group_cache
*pnhgc
;
771 memset(&find
, 0, sizeof(find
));
772 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
773 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
777 "%s: Could not find nexthop-group cache w/ name '%s'",
778 __PRETTY_FUNCTION__
, name
);
782 return pnhgc
->table_id
;
785 bool pbr_nht_get_installed(const char *name
)
787 struct pbr_nexthop_group_cache find
;
788 struct pbr_nexthop_group_cache
*pnhgc
;
790 memset(&find
, 0, sizeof(find
));
791 snprintf(find
.name
, sizeof(find
.name
), "%s", name
);
793 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
798 return pnhgc
->installed
;
801 static void pbr_nht_show_nhg_nexthops(struct hash_backet
*b
, void *data
)
803 struct pbr_nexthop_cache
*pnhc
= b
->data
;
804 struct vty
*vty
= data
;
806 vty_out(vty
, "\tValid: %d ", pnhc
->valid
);
807 nexthop_group_write_nexthop(vty
, pnhc
->nexthop
);
810 struct pbr_nht_show
{
815 static void pbr_nht_show_nhg(struct hash_backet
*b
, void *data
)
817 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
818 struct pbr_nht_show
*pns
= data
;
821 if (pns
->name
&& strcmp(pns
->name
, pnhgc
->name
) != 0)
825 vty_out(vty
, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n",
826 pnhgc
->name
, pnhgc
->table_id
, pnhgc
->valid
, pnhgc
->installed
);
828 hash_iterate(pnhgc
->nhh
, pbr_nht_show_nhg_nexthops
, vty
);
831 void pbr_nht_show_nexthop_group(struct vty
*vty
, const char *name
)
833 struct pbr_nht_show pns
;
838 hash_iterate(pbr_nhg_hash
, pbr_nht_show_nhg
, &pns
);
841 void pbr_nht_init(void)
843 pbr_nhg_hash
= hash_create_size(
844 16, pbr_nhg_hash_key
, pbr_nhg_hash_equal
, "PBR NHG Cache Hash");
846 hash_create_size(16, (unsigned int (*)(void *))nexthop_hash
,
847 pbr_nhrc_hash_equal
, "PBR NH Hash");
849 pbr_nhg_low_table
= PBR_NHT_DEFAULT_LOW_TABLEID
;
850 pbr_nhg_high_table
= PBR_NHT_DEFAULT_HIGH_TABLEID
;
851 pbr_nhg_low_rule
= PBR_NHT_DEFAULT_LOW_RULE
;
852 pbr_nhg_high_rule
= PBR_NHT_DEFAULT_HIGH_RULE
;
853 memset(&nhg_tableid
, 0, 65535 * sizeof(uint8_t));