mirror of
https://github.com/FRRouting/frr.git
synced 2025-04-30 21:47:15 +02:00
fpm: Add functions to encode nexthop in protobuf
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>
This commit is contained in:
parent
4ec8ef5098
commit
b1f1cb9c23
92
fpm/fpm_pb.h
92
fpm/fpm_pb.h
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue