]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib,sharpd: align zapi NHG apis a bit
authorStephen Worley <sworley@cumulusnetworks.com>
Tue, 22 Sep 2020 19:27:35 +0000 (15:27 -0400)
committerStephen Worley <sworley@cumulusnetworks.com>
Mon, 28 Sep 2020 16:41:00 +0000 (12:41 -0400)
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 <sworley@cumulusnetworks.com>
lib/zclient.c
lib/zclient.h
sharpd/sharp_zebra.c

index dfb409426db2881fef549d5b7e611c6ec870da8f..13eba4c7901b99cbc9240ae009a0e605e1fc304c 100644 (file)
@@ -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)
index 0fbe1de290e8180c214f77f1726172d0e0a31b42..b41f291554d5fbaa19885dd4500a52829fe07269 100644 (file)
@@ -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
 
index edbc7460e0956960f43d0a9563e2804204365592..50129c2363f4eb8f03a1937af69159995bf6e364 100644 (file)
@@ -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,