From c6ce9334b5387f482af4112ed1ffe6a49debe352 Mon Sep 17 00:00:00 2001 From: Stephen Worley Date: Tue, 22 Sep 2020 15:27:35 -0400 Subject: [PATCH] lib,sharpd: align zapi NHG apis a bit Align the zapi NHG apis to be more consistent with the zapi_route apis. Add a struct zapi_nhg to use for encodings as well. Signed-off-by: Stephen Worley --- lib/zclient.c | 53 ++++++++++++++++++++------------------------ lib/zclient.h | 14 +++++++++--- sharpd/sharp_zebra.c | 21 +++++++++--------- 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/lib/zclient.c b/lib/zclient.c index dfb409426d..13eba4c790 100644 --- a/lib/zclient.c +++ b/lib/zclient.c @@ -1017,49 +1017,44 @@ done: return ret; } -extern void zclient_nhg_del(struct zclient *zclient, uint32_t id) +static int zapi_nhg_encode(struct stream *s, uint16_t proto, int cmd, + struct zapi_nhg *api_nhg) { - struct stream *s = zclient->obuf; + if (cmd != ZEBRA_NHG_DEL && cmd != ZEBRA_NHG_ADD) { + flog_err(EC_LIB_ZAPI_ENCODE, + "%s: Specified zapi NHG command (%d) doesn't exist\n", + __func__, cmd); + return -1; + } stream_reset(s); - zclient_create_header(s, ZEBRA_NHG_DEL, VRF_DEFAULT); - - stream_putw(s, zclient->redist_default); - stream_putl(s, id); - - stream_putw_at(s, 0, stream_get_endp(s)); -} - -static void zclient_nhg_writer(struct stream *s, uint16_t proto, int cmd, - uint32_t id, size_t nhops, - struct zapi_nexthop *znh) -{ - size_t i; + zclient_create_header(s, cmd, VRF_DEFAULT); - stream_reset(s); + stream_putw(s, proto); + stream_putl(s, api_nhg->id); - zapi_nexthop_group_sort(znh, nhops); + if (cmd == ZEBRA_NHG_ADD) { + zapi_nexthop_group_sort(api_nhg->nexthops, + api_nhg->nexthop_num); - zclient_create_header(s, cmd, VRF_DEFAULT); + stream_putw(s, api_nhg->nexthop_num); - stream_putw(s, proto); - stream_putl(s, id); - stream_putw(s, nhops); - for (i = 0; i < nhops; i++) { - zapi_nexthop_encode(s, znh, 0, 0); - znh++; + for (int i = 0; i < api_nhg->nexthop_num; i++) + zapi_nexthop_encode(s, &api_nhg->nexthops[i], 0, 0); } stream_putw_at(s, 0, stream_get_endp(s)); + + return 0; } -extern void zclient_nhg_add(struct zclient *zclient, uint32_t id, size_t nhops, - struct zapi_nexthop *znh) +int zclient_nhg_send(struct zclient *zclient, int cmd, struct zapi_nhg *api_nhg) { - struct stream *s = zclient->obuf; + if (zapi_nhg_encode(zclient->obuf, zclient->redist_default, cmd, + api_nhg)) + return -1; - zclient_nhg_writer(s, zclient->redist_default, ZEBRA_NHG_ADD, id, nhops, - znh); + return zclient_send_message(zclient); } int zapi_route_encode(uint8_t cmd, struct stream *s, struct zapi_route *api) diff --git a/lib/zclient.h b/lib/zclient.h index 0fbe1de290..b41f291554 100644 --- a/lib/zclient.h +++ b/lib/zclient.h @@ -438,6 +438,15 @@ struct zapi_nexthop { #define ZAPI_NEXTHOP_FLAG_WEIGHT 0x04 #define ZAPI_NEXTHOP_FLAG_HAS_BACKUP 0x08 /* Nexthop has a backup */ +/* + * ZAPI Nexthop Group. For use with protocol creation of nexthop groups. + */ +struct zapi_nhg { + uint32_t id; + uint16_t nexthop_num; + struct zapi_nexthop nexthops[MULTIPATH_NUM]; +}; + /* * Some of these data structures do not map easily to * a actual data structure size giving different compilers @@ -898,9 +907,8 @@ bool zapi_ipset_notify_decode(struct stream *s, uint32_t *unique, enum zapi_ipset_notify_owner *note); -extern void zclient_nhg_add(struct zclient *zclient, uint32_t id, size_t nhops, - struct zapi_nexthop *znh); -extern void zclient_nhg_del(struct zclient *zclient, uint32_t id); +extern int zclient_nhg_send(struct zclient *zclient, int cmd, + struct zapi_nhg *api_nhg); #define ZEBRA_IPSET_NAME_SIZE 32 diff --git a/sharpd/sharp_zebra.c b/sharpd/sharp_zebra.c index edbc7460e0..50129c2363 100644 --- a/sharpd/sharp_zebra.c +++ b/sharpd/sharp_zebra.c @@ -359,34 +359,35 @@ void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label) void nhg_add(uint32_t id, const struct nexthop_group *nhg) { - struct zapi_nexthop nh_array[MULTIPATH_NUM]; + struct zapi_nhg api_nhg = {}; struct zapi_nexthop *api_nh; - uint16_t nexthop_num = 0; struct nexthop *nh; + api_nhg.id = id; for (ALL_NEXTHOPS_PTR(nhg, nh)) { - if (nexthop_num >= MULTIPATH_NUM) { + if (api_nhg.nexthop_num >= MULTIPATH_NUM) { zlog_warn( "%s: number of nexthops greater than max multipath size, truncating", __func__); break; } - api_nh = &nh_array[nexthop_num]; + api_nh = &api_nhg.nexthops[api_nhg.nexthop_num]; zapi_nexthop_from_nexthop(api_nh, nh); - nexthop_num++; + api_nhg.nexthop_num++; } - zclient_nhg_add(zclient, id, nexthop_num, nh_array); - - zclient_send_message(zclient); + zclient_nhg_send(zclient, ZEBRA_NHG_ADD, &api_nhg); } void nhg_del(uint32_t id) { - zclient_nhg_del(zclient, id); - zclient_send_message(zclient); + struct zapi_nhg api_nhg = {}; + + api_nhg.id = id; + + zclient_nhg_send(zclient, ZEBRA_NHG_DEL, &api_nhg); } void route_add(const struct prefix *p, vrf_id_t vrf_id, uint8_t instance, -- 2.39.5