]> git.puffer.fish Git - mirror/frr.git/commitdiff
fpm: Add functions to encode nexthop in protobuf
authorCarmine Scarpitta <cscarpit@cisco.com>
Tue, 13 Feb 2024 10:40:49 +0000 (11:40 +0100)
committerCarmine Scarpitta <cscarpit@cisco.com>
Sun, 18 Feb 2024 17:31:13 +0000 (18:31 +0100)
Add two helper functions to encode/decode nexthops in protobuf.

Specifically,
* `fpm_nexthop_create`: encode a `struct nexthop` in a protobuf nexthop
structure
* `fpm_nexthop_get`: decode a nexthop protobuf structure into a `struct
nexthop`

This is a preliminary commit to support sending SRv6 Local SIDs and VPN
SIDs via protobuf.

Signed-off-by: Carmine Scarpitta <cscarpit@cisco.com>
fpm/fpm_pb.h

index 7e39054d19257dff7c57d356f2dc6f145168e3ca..427948d1ff892475c276b8df97e70f81804febe9 100644 (file)
@@ -15,6 +15,7 @@
 #define _FPM_PB_H
 
 #include "lib/route_types.h"
+#include "lib/vrf.h"
 #include "qpb/qpb.h"
 
 #include "fpm/fpm.pb-c.h"
@@ -42,4 +43,95 @@ static inline Fpm__RouteKey *fpm__route_key__create(qpb_allocator_t *allocator,
        return key;
 }
 
+/*
+ * fpm__nexthop__create
+ */
+#define fpm_nexthop_create fpm__nexthop__create
+static inline Fpm__Nexthop *
+fpm__nexthop__create(qpb_allocator_t *allocator, struct nexthop *nh)
+{
+       Fpm__Nexthop *nexthop;
+       uint8_t family;
+
+       nexthop = QPB_ALLOC(allocator, typeof(*nexthop));
+       if (!nexthop)
+               return NULL;
+
+       fpm__nexthop__init(nexthop);
+
+       if (nh->type == NEXTHOP_TYPE_IPV4 ||
+           nh->type == NEXTHOP_TYPE_IPV4_IFINDEX)
+               family = AF_INET;
+       else if (nh->type == NEXTHOP_TYPE_IPV6 ||
+                nh->type == NEXTHOP_TYPE_IPV6_IFINDEX)
+               family = AF_INET6;
+       else
+               return NULL;
+
+       nexthop->if_id = qpb__if_identifier__create(allocator, nh->ifindex);
+       if (!nexthop->if_id)
+               return NULL;
+
+       nexthop->address = qpb__l3_address__create(allocator, &nh->gate, family);
+       if (!nexthop->address)
+               return NULL;
+
+
+       return nexthop;
+}
+
+/*
+ * fpm__nexthop__get
+ *
+ * Read out information from a protobuf nexthop structure.
+ */
+#define fpm_nexthop_get fpm__nexthop__get
+static inline int fpm__nexthop__get(const Fpm__Nexthop *nh,
+                                   struct nexthop *nexthop)
+{
+       struct in_addr ipv4;
+       struct in6_addr ipv6;
+       uint32_t ifindex;
+       char *ifname;
+
+       if (!nh)
+               return 0;
+
+       if (!qpb_if_identifier_get(nh->if_id, &ifindex, &ifname))
+               return 0;
+
+       if (nh->address) {
+               if (nh->address->v4) {
+                       memset(&ipv4, 0, sizeof(ipv4));
+                       if (!qpb__ipv4_address__get(nh->address->v4, &ipv4))
+                               return 0;
+
+                       nexthop->vrf_id = VRF_DEFAULT;
+                       nexthop->type = NEXTHOP_TYPE_IPV4;
+                       nexthop->gate.ipv4 = ipv4;
+                       if (ifindex) {
+                               nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
+                               nexthop->ifindex = ifindex;
+                       }
+                       return 1;
+               }
+
+               if (nh->address->v6) {
+                       memset(&ipv6, 0, sizeof(ipv6));
+                       if (!qpb__ipv6_address__get(nh->address->v6, &ipv6))
+                               return 0;
+                       nexthop->vrf_id = VRF_DEFAULT;
+                       nexthop->type = NEXTHOP_TYPE_IPV6;
+                       nexthop->gate.ipv6 = ipv6;
+                       if (ifindex) {
+                               nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
+                               nexthop->ifindex = ifindex;
+                       }
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
 #endif