Skip to content

Commit

Permalink
fpm: Add functions to encode nexthop in protobuf
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cscarpitta committed Feb 13, 2024
1 parent 152579f commit f957330
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions fpm/fpm_pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

0 comments on commit f957330

Please sign in to comment.