From 50d8965075cf59744dde685ae01400d9b7ee08a2 Mon Sep 17 00:00:00 2001 From: Stephen Worley Date: Mon, 24 Jun 2019 14:04:13 -0400 Subject: [PATCH] lib: Private api for nexthop_group manipulation Add a file that exposes functions which modify nexthop groups. Nexthop groups are techincally immutable but there are a few special cases where we need direct access to add/remove nexthops after the group has been made. This file provides a way to expose those functions in a way that makes it clear this is a private/hidden api. Signed-off-by: Stephen Worley --- lib/nexthop_group.c | 26 ++++++++++----------- lib/nexthop_group.h | 4 ---- lib/nexthop_group_private.h | 45 +++++++++++++++++++++++++++++++++++++ lib/subdir.am | 1 + pbrd/pbr_nht.c | 7 +++--- pbrd/pbr_vty.c | 3 ++- zebra/zebra_nhg.c | 3 ++- zebra/zebra_rib.c | 5 +++-- 8 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 lib/nexthop_group_private.h diff --git a/lib/nexthop_group.c b/lib/nexthop_group.c index 7b3fb15625..5602018b30 100644 --- a/lib/nexthop_group.c +++ b/lib/nexthop_group.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -112,7 +113,7 @@ void nexthop_group_delete(struct nexthop_group **nhg) } /* Add nexthop to the end of a nexthop list. */ -void nexthop_add(struct nexthop **target, struct nexthop *nexthop) +void _nexthop_add(struct nexthop **target, struct nexthop *nexthop) { struct nexthop *last; @@ -125,8 +126,8 @@ void nexthop_add(struct nexthop **target, struct nexthop *nexthop) nexthop->prev = last; } -void nexthop_group_add_sorted(struct nexthop_group *nhg, - struct nexthop *nexthop) +void _nexthop_group_add_sorted(struct nexthop_group *nhg, + struct nexthop *nexthop) { struct nexthop *position, *prev; @@ -151,11 +152,10 @@ void nexthop_group_add_sorted(struct nexthop_group *nhg, prev->next = nexthop; else nhg->nexthop = nexthop; - } /* Delete nexthop from a nexthop list. */ -void nexthop_del(struct nexthop_group *nhg, struct nexthop *nh) +void _nexthop_del(struct nexthop_group *nhg, struct nexthop *nh) { struct nexthop *nexthop; @@ -186,7 +186,7 @@ void copy_nexthops(struct nexthop **tnh, const struct nexthop *nh, for (nh1 = nh; nh1; nh1 = nh1->next) { nexthop = nexthop_dup(nh1, rparent); - nexthop_add(tnh, nexthop); + _nexthop_add(tnh, nexthop); if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE)) copy_nexthops(&nexthop->resolved, nh1->resolved, @@ -217,7 +217,7 @@ static void nhgc_delete_nexthops(struct nexthop_group_cmd *nhgc) while (nexthop) { struct nexthop *next = nexthop_next(nexthop); - nexthop_del(&nhgc->nhg, nexthop); + _nexthop_del(&nhgc->nhg, nexthop); if (nhg_hooks.del_nexthop) nhg_hooks.del_nexthop(nhgc, nexthop); @@ -481,7 +481,7 @@ DEFPY(ecmp_nexthops, ecmp_nexthops_cmd, if (no) { nexthop_group_unsave_nhop(nhgc, name, addr, intf); if (nh) { - nexthop_del(&nhgc->nhg, nh); + _nexthop_del(&nhgc->nhg, nh); if (nhg_hooks.del_nexthop) nhg_hooks.del_nexthop(nhgc, nh); @@ -494,7 +494,7 @@ DEFPY(ecmp_nexthops, ecmp_nexthops_cmd, nh = nexthop_new(); memcpy(nh, &nhop, sizeof(nhop)); - nexthop_add(&nhgc->nhg.nexthop, nh); + _nexthop_add(&nhgc->nhg.nexthop, nh); } nexthop_group_save_nhop(nhgc, name, addr, intf); @@ -618,7 +618,7 @@ void nexthop_group_enable_vrf(struct vrf *vrf) nh = nexthop_new(); memcpy(nh, &nhop, sizeof(nhop)); - nexthop_add(&nhgc->nhg.nexthop, nh); + _nexthop_add(&nhgc->nhg.nexthop, nh); if (nhg_hooks.add_nexthop) nhg_hooks.add_nexthop(nhgc, nh); @@ -651,7 +651,7 @@ void nexthop_group_disable_vrf(struct vrf *vrf) if (nh->vrf_id != vrf->vrf_id) continue; - nexthop_del(&nhgc->nhg, nh); + _nexthop_del(&nhgc->nhg, nh); if (nhg_hooks.del_nexthop) nhg_hooks.del_nexthop(nhgc, nh); @@ -701,7 +701,7 @@ void nexthop_group_interface_state_change(struct interface *ifp, nh = nexthop_new(); memcpy(nh, &nhop, sizeof(nhop)); - nexthop_add(&nhgc->nhg.nexthop, nh); + _nexthop_add(&nhgc->nhg.nexthop, nh); if (nhg_hooks.add_nexthop) nhg_hooks.add_nexthop(nhgc, nh); @@ -725,7 +725,7 @@ void nexthop_group_interface_state_change(struct interface *ifp, if (oldifindex != nh->ifindex) continue; - nexthop_del(&nhgc->nhg, nh); + _nexthop_del(&nhgc->nhg, nh); if (nhg_hooks.del_nexthop) nhg_hooks.del_nexthop(nhgc, nh); diff --git a/lib/nexthop_group.h b/lib/nexthop_group.h index 104e2051f5..4f4d40eb33 100644 --- a/lib/nexthop_group.h +++ b/lib/nexthop_group.h @@ -42,12 +42,8 @@ struct nexthop_group { struct nexthop_group *nexthop_group_new(void); void nexthop_group_delete(struct nexthop_group **nhg); -void nexthop_add(struct nexthop **target, struct nexthop *nexthop); -void nexthop_group_add_sorted(struct nexthop_group *nhg, - struct nexthop *nexthop); void nexthop_group_copy(struct nexthop_group *to, struct nexthop_group *from); -void nexthop_del(struct nexthop_group *nhg, struct nexthop *nexthop); void copy_nexthops(struct nexthop **tnh, const struct nexthop *nh, struct nexthop *rparent); diff --git a/lib/nexthop_group_private.h b/lib/nexthop_group_private.h new file mode 100644 index 0000000000..cdd0df0ab3 --- /dev/null +++ b/lib/nexthop_group_private.h @@ -0,0 +1,45 @@ +/* + * Nexthop Group Private Functions. + * Copyright (C) 2019 Cumulus Networks, Inc. + * Stephen Worley + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * These functions should only be used internally for nexthop groups + * and in certain special cases. Please use `lib/nexthop_group.h` for + * any general nexthop_group api needs. + */ + +#ifndef __NEXTHOP_GROUP_PRIVATE__ +#define __NEXTHOP_GROUP_PRIVATE__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void _nexthop_add(struct nexthop **target, struct nexthop *nexthop); +void _nexthop_del(struct nexthop_group *nhg, struct nexthop *nexthop); +void _nexthop_group_add_sorted(struct nexthop_group *nhg, + struct nexthop *nexthop); + +#ifdef __cplusplus +} +#endif + +#endif /* __NEXTHOP_GROUP_PRIVATE__ */ diff --git a/lib/subdir.am b/lib/subdir.am index 8b6cbe2aeb..8223bd547c 100644 --- a/lib/subdir.am +++ b/lib/subdir.am @@ -188,6 +188,7 @@ pkginclude_HEADERS += \ lib/network.h \ lib/nexthop.h \ lib/nexthop_group.h \ + lib/nexthop_group_private.h \ lib/northbound.h \ lib/northbound_cli.h \ lib/northbound_db.h \ diff --git a/pbrd/pbr_nht.c b/pbrd/pbr_nht.c index 22dd6f1a38..a69bb00848 100644 --- a/pbrd/pbr_nht.c +++ b/pbrd/pbr_nht.c @@ -21,7 +21,8 @@ #include #include -#include +#include "nexthop_group.h" +#include "nexthop_group_private.h" #include #include #include @@ -576,7 +577,7 @@ void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence *pbrms) hash_release(pbr_nhg_hash, pnhgc); - nexthop_del(pbrms->nhg, nh); + _nexthop_del(pbrms->nhg, nh); nexthop_free(nh); nexthop_group_delete(&pbrms->nhg); XFREE(MTYPE_TMP, pbrms->internal_nhg_name); @@ -723,7 +724,7 @@ static void pbr_nexthop_group_cache_iterate_to_group(struct hash_bucket *b, copy_nexthops(&nh, pnhc->nexthop, NULL); - nexthop_add(&nhg->nexthop, nh); + _nexthop_add(&nhg->nexthop, nh); } static void diff --git a/pbrd/pbr_vty.c b/pbrd/pbr_vty.c index a71c712ea7..95f38563b1 100644 --- a/pbrd/pbr_vty.c +++ b/pbrd/pbr_vty.c @@ -25,6 +25,7 @@ #include "vrf.h" #include "nexthop.h" #include "nexthop_group.h" +#include "nexthop_group_private.h" #include "log.h" #include "debug.h" #include "pbr.h" @@ -329,7 +330,7 @@ DEFPY(pbr_map_nexthop, pbr_map_nexthop_cmd, nh = nexthop_new(); memcpy(nh, &nhop, sizeof(nhop)); - nexthop_add(&pbrms->nhg->nexthop, nh); + _nexthop_add(&pbrms->nhg->nexthop, nh); pbr_nht_add_individual_nexthop(pbrms); pbr_map_check(pbrms); diff --git a/zebra/zebra_nhg.c b/zebra/zebra_nhg.c index f2a76d1c52..4a88296051 100644 --- a/zebra/zebra_nhg.c +++ b/zebra/zebra_nhg.c @@ -23,6 +23,7 @@ #include #include "lib/nexthop.h" +#include "lib/nexthop_group_private.h" #include "lib/routemap.h" #include "zebra/connected.h" @@ -100,7 +101,7 @@ static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop, &newhop->nh_label->label[0]); resolved_hop->rparent = nexthop; - nexthop_add(&nexthop->resolved, resolved_hop); + _nexthop_add(&nexthop->resolved, resolved_hop); } /* diff --git a/zebra/zebra_rib.c b/zebra/zebra_rib.c index 2df24f75c5..5b0ca6570e 100644 --- a/zebra/zebra_rib.c +++ b/zebra/zebra_rib.c @@ -36,6 +36,7 @@ #include "thread.h" #include "vrf.h" #include "workqueue.h" +#include "nexthop_group_private.h" #include "zebra/zebra_router.h" #include "zebra/connected.h" @@ -192,7 +193,7 @@ int zebra_check_addr(const struct prefix *p) /* Add nexthop to the end of a rib node's nexthop list */ void route_entry_nexthop_add(struct route_entry *re, struct nexthop *nexthop) { - nexthop_add(&re->ng.nexthop, nexthop); + _nexthop_add(&re->ng.nexthop, nexthop); re->nexthop_num++; } @@ -1589,7 +1590,7 @@ static bool rib_update_re_from_ctx(struct route_entry *re, */ nexthop = nexthop_new(); nexthop->type = NEXTHOP_TYPE_IPV4; - nexthop_add(&(re->fib_ng.nexthop), nexthop); + _nexthop_add(&(re->fib_ng.nexthop), nexthop); } done: -- 2.39.5