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
);
212 pnhgc
= pbr_nht_add_group(name
);
214 DEBUGD(&pbr_dbg_nht
, "%s: Added nexthop-group %s", __PRETTY_FUNCTION__
,
217 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
218 pbr_map_check_nh_group_change(name
);
221 void pbr_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
222 const struct nexthop
*nhop
)
225 struct pbr_nexthop_group_cache pnhgc_find
= {};
226 struct pbr_nexthop_group_cache
*pnhgc
;
227 struct pbr_nexthop_cache pnhc_find
= {};
228 struct pbr_nexthop_cache
*pnhc
;
230 /* find pnhgc by name */
231 strlcpy(pnhgc_find
.name
, nhgc
->name
, sizeof(pnhgc_find
.name
));
232 pnhgc
= hash_get(pbr_nhg_hash
, &pnhgc_find
, pbr_nhgc_alloc
);
234 /* create & insert new pnhc into pnhgc->nhh */
235 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
236 pnhc
= hash_get(pnhgc
->nhh
, &pnhc_find
, pbr_nh_alloc
);
237 pnhc_find
.nexthop
= NULL
;
239 /* set parent pnhgc */
240 pnhc
->parent
= pnhgc
;
242 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
243 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
244 DEBUGD(&pbr_dbg_nht
, "%s: Added %s to nexthop-group %s",
245 __PRETTY_FUNCTION__
, debugstr
, nhgc
->name
);
248 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
249 pbr_map_check_nh_group_change(nhgc
->name
);
252 void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
253 const struct nexthop
*nhop
)
256 struct pbr_nexthop_group_cache pnhgc_find
= {};
257 struct pbr_nexthop_group_cache
*pnhgc
;
258 struct pbr_nexthop_cache pnhc_find
= {};
259 struct pbr_nexthop_cache
*pnhc
;
260 enum nexthop_types_t nh_afi
= nhop
->type
;
262 /* find pnhgc by name */
263 strlcpy(pnhgc_find
.name
, nhgc
->name
, sizeof(pnhgc_find
.name
));
264 pnhgc
= hash_get(pbr_nhg_hash
, &pnhgc_find
, pbr_nhgc_alloc
);
266 /* delete pnhc from pnhgc->nhh */
267 pnhc_find
.nexthop
= (struct nexthop
*)nhop
;
268 pnhc
= hash_release(pnhgc
->nhh
, &pnhc_find
);
271 pbr_nh_delete(&pnhc
);
273 if (DEBUG_MODE_CHECK(&pbr_dbg_nht
, DEBUG_MODE_ALL
)) {
274 nexthop2str(nhop
, debugstr
, sizeof(debugstr
));
275 DEBUGD(&pbr_dbg_nht
, "%s: Removed %s from nexthop-group %s",
276 __PRETTY_FUNCTION__
, debugstr
, nhgc
->name
);
279 if (pnhgc
->nhh
->count
)
280 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
282 pbr_nht_uninstall_nexthop_group(pnhgc
, nhgc
->nhg
, nh_afi
);
284 pbr_map_check_nh_group_change(nhgc
->name
);
287 void pbr_nhgroup_delete_cb(const char *name
)
289 DEBUGD(&pbr_dbg_nht
, "%s: Removed nexthop-group %s",
290 __PRETTY_FUNCTION__
, name
);
292 /* delete group from all pbrms's */
293 pbr_nht_delete_group(name
);
295 pbr_map_check_nh_group_change(name
);
299 static struct pbr_nexthop_cache
*pbr_nht_lookup_nexthop(struct nexthop
*nexthop
)
305 static void pbr_nht_find_nhg_from_table_install(struct hash_backet
*b
,
308 struct pbr_nexthop_group_cache
*pnhgc
=
309 (struct pbr_nexthop_group_cache
*)b
->data
;
310 uint32_t *table_id
= (uint32_t *)data
;
312 if (pnhgc
->table_id
== *table_id
) {
313 DEBUGD(&pbr_dbg_nht
, "%s: Table ID (%u) matches %s",
314 __PRETTY_FUNCTION__
, *table_id
, pnhgc
->name
);
317 * If the table has been re-handled by zebra
318 * and we are already installed no need to do
321 if (!pnhgc
->installed
) {
322 pnhgc
->installed
= true;
323 pbr_map_schedule_policy_from_nhg(pnhgc
->name
);
328 void pbr_nht_route_installed_for_table(uint32_t table_id
)
330 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_install
,
334 static void pbr_nht_find_nhg_from_table_remove(struct hash_backet
*b
,
340 void pbr_nht_route_removed_for_table(uint32_t table_id
)
342 hash_iterate(pbr_nhg_hash
, pbr_nht_find_nhg_from_table_remove
,
347 * Loop through all nexthops in a nexthop group to check that they are all the
348 * same. If they are not all the same, log this peculiarity.
351 * The nexthop group to check
354 * - AFI of last nexthop in the group
357 static afi_t
pbr_nht_which_afi(struct nexthop_group nhg
,
358 enum nexthop_types_t nh_afi
)
360 struct nexthop
*nexthop
;
361 afi_t install_afi
= AFI_MAX
;
364 v6
= v4
= bh
= false;
367 for (ALL_NEXTHOPS(nhg
, nexthop
)) {
368 nh_afi
= nexthop
->type
;
374 case NEXTHOP_TYPE_IFINDEX
:
376 case NEXTHOP_TYPE_IPV4
:
377 case NEXTHOP_TYPE_IPV4_IFINDEX
:
379 install_afi
= AFI_IP
;
381 case NEXTHOP_TYPE_IPV6
:
382 case NEXTHOP_TYPE_IPV6_IFINDEX
:
384 install_afi
= AFI_IP6
;
386 case NEXTHOP_TYPE_BLACKHOLE
:
388 install_afi
= AFI_MAX
;
394 "%s: Saw both V6 and V4 nexthops...using %s",
395 __PRETTY_FUNCTION__
, afi2str(install_afi
));
396 if (bh
&& (v6
|| v4
))
398 "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.",
399 __PRETTY_FUNCTION__
, v4
? "v4" : "",
400 (v4
&& v6
) ? " and " : "", v6
? "v6" : "");
405 static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
406 struct nexthop_group nhg
)
409 enum nexthop_types_t nh_afi
= 0;
411 install_afi
= pbr_nht_which_afi(nhg
, nh_afi
);
413 route_add(pnhgc
, nhg
, install_afi
);
417 pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache
*pnhgc
,
418 struct nexthop_group nhg
,
419 enum nexthop_types_t nh_afi
)
423 install_afi
= pbr_nht_which_afi(nhg
, nh_afi
);
425 pnhgc
->installed
= false;
426 pnhgc
->valid
= false;
427 route_delete(pnhgc
, install_afi
);
430 void pbr_nht_change_group(const char *name
)
432 struct nexthop_group_cmd
*nhgc
;
433 struct pbr_nexthop_group_cache
*pnhgc
;
434 struct pbr_nexthop_group_cache find
;
435 struct nexthop
*nhop
;
437 nhgc
= nhgc_find(name
);
441 memset(&find
, 0, sizeof(find
));
442 strcpy(find
.name
, name
);
443 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
447 "%s: Could not find nexthop-group cache w/ name '%s'",
448 __PRETTY_FUNCTION__
, name
);
452 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
453 struct pbr_nexthop_cache lookup
;
454 struct pbr_nexthop_cache
*pnhc
;
456 lookup
.nexthop
= nhop
;
457 pnhc
= hash_lookup(pnhgc
->nhh
, &lookup
);
459 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
460 pnhc
->parent
= pnhgc
;
463 pbr_nht_install_nexthop_group(pnhgc
, nhgc
->nhg
);
466 char *pbr_nht_nexthop_make_name(char *name
, size_t l
,
467 uint32_t seqno
, char *buffer
)
469 snprintf(buffer
, l
, "%s%u", name
, seqno
);
473 void pbr_nht_add_individual_nexthop(struct pbr_map_sequence
*pbrms
)
475 struct pbr_nexthop_group_cache
*pnhgc
;
476 struct pbr_nexthop_group_cache find
;
477 struct pbr_nexthop_cache
*pnhc
;
478 struct pbr_nexthop_cache lookup
;
480 memset(&find
, 0, sizeof(find
));
481 pbr_nht_nexthop_make_name(pbrms
->parent
->name
, PBR_MAP_NAMELEN
,
482 pbrms
->seqno
, find
.name
);
483 if (!pbrms
->internal_nhg_name
)
484 pbrms
->internal_nhg_name
= XSTRDUP(MTYPE_TMP
, find
.name
);
486 pnhgc
= hash_get(pbr_nhg_hash
, &find
, pbr_nhgc_alloc
);
488 lookup
.nexthop
= pbrms
->nhg
->nexthop
;
489 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
490 pnhc
->parent
= pnhgc
;
491 pbr_nht_install_nexthop_group(pnhgc
, *pbrms
->nhg
);
494 void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence
*pbrms
)
496 struct pbr_nexthop_group_cache
*pnhgc
;
497 struct pbr_nexthop_group_cache find
;
498 struct pbr_nexthop_cache
*pnhc
;
499 struct pbr_nexthop_cache lup
;
500 struct pbr_map
*pbrm
= pbrms
->parent
;
501 struct listnode
*node
;
502 struct pbr_map_interface
*pmi
;
504 enum nexthop_types_t nh_afi
= 0;
506 if (pbrm
->valid
&& pbrms
->nhs_installed
&& pbrm
->incoming
->count
) {
507 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, node
, pmi
))
508 pbr_send_pbr_map(pbrms
, pmi
, false);
512 pbrms
->nhs_installed
= false;
513 pbrms
->installed
= false;
514 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
516 memset(&find
, 0, sizeof(find
));
517 strcpy(&find
.name
[0], pbrms
->internal_nhg_name
);
518 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
520 nh
= pbrms
->nhg
->nexthop
;
523 pnhc
= hash_lookup(pnhgc
->nhh
, &lup
);
525 hash_release(pnhgc
->nhh
, pnhc
);
526 pbr_nh_delete(&pnhc
);
527 pbr_nht_uninstall_nexthop_group(pnhgc
, *pbrms
->nhg
, nh_afi
);
529 hash_release(pbr_nhg_hash
, pnhgc
);
531 nexthop_del(pbrms
->nhg
, nh
);
533 nexthop_group_delete(&pbrms
->nhg
);
534 XFREE(MTYPE_TMP
, pbrms
->internal_nhg_name
);
537 struct pbr_nexthop_group_cache
*pbr_nht_add_group(const char *name
)
539 struct nexthop
*nhop
;
540 struct nexthop_group_cmd
*nhgc
;
541 struct pbr_nexthop_group_cache
*pnhgc
;
542 struct pbr_nexthop_group_cache lookup
;
544 nhgc
= nhgc_find(name
);
547 zlog_warn("%s: Could not find group %s to add",
548 __PRETTY_FUNCTION__
, name
);
552 strcpy(lookup
.name
, name
);
553 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, pbr_nhgc_alloc
);
554 DEBUGD(&pbr_dbg_nht
, "%s: Retrieved NHGC @ %p", __PRETTY_FUNCTION__
,
557 for (ALL_NEXTHOPS(nhgc
->nhg
, nhop
)) {
558 struct pbr_nexthop_cache lookup
;
559 struct pbr_nexthop_cache
*pnhc
;
561 lookup
.nexthop
= nhop
;
562 pnhc
= hash_lookup(pnhgc
->nhh
, &lookup
);
564 pnhc
= hash_get(pnhgc
->nhh
, &lookup
, pbr_nh_alloc
);
565 pnhc
->parent
= pnhgc
;
572 void pbr_nht_delete_group(const char *name
)
574 struct pbr_map_sequence
*pbrms
;
575 struct listnode
*snode
;
576 struct pbr_map
*pbrm
;
577 struct pbr_nexthop_group_cache pnhgc_find
;
578 struct pbr_nexthop_group_cache
*pnhgc
;
580 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
581 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, snode
, pbrms
)) {
582 if (pbrms
->nhgrp_name
583 && strmatch(pbrms
->nhgrp_name
, name
)) {
584 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
585 nexthop_group_delete(&pbrms
->nhg
);
587 pbrms
->internal_nhg_name
= NULL
;
593 strlcpy(pnhgc_find
.name
, name
, sizeof(pnhgc_find
.name
));
594 pnhgc
= hash_release(pbr_nhg_hash
, &pnhgc_find
);
595 pbr_nhgc_delete(pnhgc
);
598 bool pbr_nht_nexthop_valid(struct nexthop_group
*nhg
)
600 DEBUGD(&pbr_dbg_nht
, "%s: %p", __PRETTY_FUNCTION__
, nhg
);
604 bool pbr_nht_nexthop_group_valid(const char *name
)
606 struct pbr_nexthop_group_cache
*pnhgc
;
607 struct pbr_nexthop_group_cache lookup
;
609 DEBUGD(&pbr_dbg_nht
, "%s: %s", __PRETTY_FUNCTION__
, name
);
611 strcpy(lookup
.name
, name
);
612 pnhgc
= hash_get(pbr_nhg_hash
, &lookup
, NULL
);
615 DEBUGD(&pbr_dbg_nht
, "%s: \t%d %d", __PRETTY_FUNCTION__
, pnhgc
->valid
,
617 if (pnhgc
->valid
&& pnhgc
->installed
)
623 struct pbr_nht_individual
{
624 struct zapi_route
*nhr
;
629 static void pbr_nht_individual_nexthop_update_lookup(struct hash_backet
*b
,
632 struct pbr_nexthop_cache
*pnhc
= b
->data
;
633 struct pbr_nht_individual
*pnhi
= data
;
634 char buf
[PREFIX_STRLEN
];
637 old_valid
= pnhc
->valid
;
639 switch (pnhi
->nhr
->prefix
.family
) {
641 if (pnhc
->nexthop
->gate
.ipv4
.s_addr
642 == pnhi
->nhr
->prefix
.u
.prefix4
.s_addr
)
643 pnhc
->valid
= !!pnhi
->nhr
->nexthop_num
;
646 if (memcmp(&pnhc
->nexthop
->gate
.ipv6
,
647 &pnhi
->nhr
->prefix
.u
.prefix6
, 16)
649 pnhc
->valid
= !!pnhi
->nhr
->nexthop_num
;
653 DEBUGD(&pbr_dbg_nht
, "\tFound %s: old: %d new: %d",
654 prefix2str(&pnhi
->nhr
->prefix
, buf
, sizeof(buf
)), old_valid
,
661 static void pbr_nht_nexthop_update_lookup(struct hash_backet
*b
, void *data
)
663 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
664 struct pbr_nht_individual pnhi
;
667 old_valid
= pnhgc
->valid
;
669 pnhi
.nhr
= (struct zapi_route
*)data
;
671 hash_iterate(pnhgc
->nhh
, pbr_nht_individual_nexthop_update_lookup
,
675 * If any of the specified nexthops are valid we are valid
677 pnhgc
->valid
= !!pnhi
.valid
;
679 if (old_valid
!= pnhgc
->valid
)
680 pbr_map_check_nh_group_change(pnhgc
->name
);
683 void pbr_nht_nexthop_update(struct zapi_route
*nhr
)
685 hash_iterate(pbr_nhg_hash
, pbr_nht_nexthop_update_lookup
, nhr
);
688 static uint32_t pbr_nhg_hash_key(void *arg
)
690 struct pbr_nexthop_group_cache
*nhgc
=
691 (struct pbr_nexthop_group_cache
*)arg
;
693 return jhash(&nhgc
->name
, strlen(nhgc
->name
), 0x52c34a96);
696 static int pbr_nhg_hash_equal(const void *arg1
, const void *arg2
)
698 const struct pbr_nexthop_group_cache
*nhgc1
=
699 (const struct pbr_nexthop_group_cache
*)arg1
;
700 const struct pbr_nexthop_group_cache
*nhgc2
=
701 (const struct pbr_nexthop_group_cache
*)arg2
;
703 return !strcmp(nhgc1
->name
, nhgc2
->name
);
707 uint32_t pbr_nht_get_next_tableid(void)
712 for (i
= pbr_nhg_low_table
; i
<= pbr_nhg_high_table
; i
++) {
713 if (nhg_tableid
[i
] == false) {
720 nhg_tableid
[i
] = true;
726 void pbr_nht_set_tableid_range(uint32_t low
, uint32_t high
)
728 pbr_nhg_low_table
= low
;
729 pbr_nhg_high_table
= high
;
732 void pbr_nht_write_table_range(struct vty
*vty
)
734 if (pbr_nhg_low_table
!= PBR_NHT_DEFAULT_LOW_TABLEID
735 || pbr_nhg_high_table
!= PBR_NHT_DEFAULT_HIGH_TABLEID
) {
736 vty_out(vty
, "pbr table range %u %u\n", pbr_nhg_low_table
,
741 uint32_t pbr_nht_get_next_rule(uint32_t seqno
)
743 return seqno
+ pbr_nhg_low_rule
- 1;
745 void pbr_nht_set_rule_range(uint32_t low
, uint32_t high
)
747 pbr_nhg_low_rule
= low
;
748 pbr_nhg_high_rule
= high
;
751 void pbr_nht_write_rule_range(struct vty
*vty
)
753 if (pbr_nhg_low_rule
!= PBR_NHT_DEFAULT_LOW_RULE
754 || pbr_nhg_high_rule
!= PBR_NHT_DEFAULT_HIGH_RULE
) {
755 vty_out(vty
, "pbr rule range %u %u\n", pbr_nhg_low_rule
,
760 uint32_t pbr_nht_get_table(const char *name
)
762 struct pbr_nexthop_group_cache find
;
763 struct pbr_nexthop_group_cache
*pnhgc
;
765 memset(&find
, 0, sizeof(find
));
766 strcpy(find
.name
, name
);
767 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
771 "%s: Could not find nexthop-group cache w/ name '%s'",
772 __PRETTY_FUNCTION__
, name
);
776 return pnhgc
->table_id
;
779 bool pbr_nht_get_installed(const char *name
)
781 struct pbr_nexthop_group_cache find
;
782 struct pbr_nexthop_group_cache
*pnhgc
;
784 memset(&find
, 0, sizeof(find
));
785 strcpy(find
.name
, name
);
787 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
792 return pnhgc
->installed
;
795 static void pbr_nht_show_nhg_nexthops(struct hash_backet
*b
, void *data
)
797 struct pbr_nexthop_cache
*pnhc
= b
->data
;
798 struct vty
*vty
= data
;
800 vty_out(vty
, "\tValid: %d ", pnhc
->valid
);
801 nexthop_group_write_nexthop(vty
, pnhc
->nexthop
);
804 struct pbr_nht_show
{
809 static void pbr_nht_show_nhg(struct hash_backet
*b
, void *data
)
811 struct pbr_nexthop_group_cache
*pnhgc
= b
->data
;
812 struct pbr_nht_show
*pns
= data
;
815 if (pns
->name
&& strcmp(pns
->name
, pnhgc
->name
) != 0)
819 vty_out(vty
, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n",
820 pnhgc
->name
, pnhgc
->table_id
, pnhgc
->valid
, pnhgc
->installed
);
822 hash_iterate(pnhgc
->nhh
, pbr_nht_show_nhg_nexthops
, vty
);
825 void pbr_nht_show_nexthop_group(struct vty
*vty
, const char *name
)
827 struct pbr_nht_show pns
;
832 hash_iterate(pbr_nhg_hash
, pbr_nht_show_nhg
, &pns
);
835 void pbr_nht_init(void)
837 pbr_nhg_hash
= hash_create_size(
838 16, pbr_nhg_hash_key
, pbr_nhg_hash_equal
, "PBR NHG Cache Hash");
840 hash_create_size(16, (unsigned int (*)(void *))nexthop_hash
,
841 pbr_nhrc_hash_equal
, "PBR NH Hash");
843 pbr_nhg_low_table
= PBR_NHT_DEFAULT_LOW_TABLEID
;
844 pbr_nhg_high_table
= PBR_NHT_DEFAULT_HIGH_TABLEID
;
845 pbr_nhg_low_rule
= PBR_NHT_DEFAULT_LOW_RULE
;
846 pbr_nhg_high_rule
= PBR_NHT_DEFAULT_HIGH_RULE
;
847 memset(&nhg_tableid
, 0, 65535 * sizeof(uint8_t));